我正在尝试使用Bookshelf在Merchants
和Transactions
之间创建关系。
我的模型定义如下:
var Voucher = Bookshelf.Model.extend({
tableName: 'vouchers',
transaction: function() {
return this.belongsTo(Transaction);
},
merchant: function() {
return this.belongsTo(Merchant);
}
});
var Transaction = Bookshelf.Model.extend({
tableName: 'transactions',
vouchers: function() {
return this.hasMany(Voucher);
}
});
var Merchant = Bookshelf.Model.extend({
tableName: 'merchants',
vouchers: function() {
return this.hasMany(Voucher);
},
transactions: function() {
//???
}
});
var schema = {
merchants: {
id: {type: 'increments', nullable: false, primary: true},
category_id: {type: 'integer', nullable: false, unsigned: true, references: 'categories.id'},
name: {type: 'string', maxlength: 150, nullable: false},
slug: {type: 'string', maxlength: 150, nullable: false, unique: true},
email: {type: 'string', maxlength: 254, nullable: false},
tel: {type: 'string', maxlength: 64, nullable: false},
location_name: {type: 'string', maxlength: 150, nullable: false},
province: {type: 'string', maxlength: 64, nullable: false},
city: {type: 'string', maxlength: 254, nullable: false},
town: {type: 'string', maxlength: 254, nullable: true},
address: {type: 'text', maxlength: 2000, nullable: false},
created_at: {type: 'dateTime', nullable: false},
updated_at: {type: 'dateTime', nullable: true}
},
transactions: {
id: {type: 'increments', nullable: false, primary: true},
user_id: {type: 'integer', nullable: false, unsigned: true, references: 'users.id'},
uuid: {type: 'string', maxlength: 128, nullable: false},
raw: {type: 'text', maxlength: 2000, nullable: false},
total_amount: {type: 'integer', nullable: false},
total_vouchers: {type: 'integer', nullable: false},
total_postages: {type: 'integer', nullable: true},
created_at: {type: 'dateTime', nullable: false},
updated_at: {type: 'dateTime', nullable: true}
},
vouchers: {
id: {type: 'increments', nullable: false, primary: true},
transaction_id: {type: 'integer', nullable: false, unsigned: true, references: 'transactions.id'},
merchant_id: {type: 'integer', nullable: false, unsigned: true, references: 'merchants.id'},
user_id: {type: 'integer', nullable: false, unsigned: true, references: 'users.id'},
vouchertemplate_id: {type: 'integer', nullable: false, unsigned: true, references: 'vouchertemplates.id'},
voucher_number: {type: 'string', maxlength: 254, nullable: false},
value: {type: 'integer', nullable: false},
redeemed_value: {type: 'integer', nullable: false, defaultTo: 0},
message: {type: 'string', maxlength: 254, nullable: false},
recipient_name: {type: 'string', maxlength: 254, nullable: false},
created_at: {type: 'dateTime', nullable: false},
updated_at: {type: 'dateTime', nullable: true}
}
};
答案 0 :(得分:0)
我认为问题出在您的架构中。无论是商家还是交易都没有任何身份证明。目前您无法加入他们。也许稍微修改架构?