我如何定义hasMany Space - >账户关系?
var Space = Bookshelf.Model.extend({
tableName : 'spaces',
// Account variable does not exist :/
});
var Account = Bookshelf.Model.extend({
tableName : 'accounts',
spaceId : function() {
return this.belongsTo(Space);
},
});
定义此方法的正确方法是什么?
P.S。书架js库没有标签:http://bookshelfjs.org/
答案 0 :(得分:8)
根据Docs,这应该有效:
var Account = Bookshelf.Model.extend({
tableName : 'accounts'
});
var Space = Bookshelf.Model.extend({
tableName : 'spaces',
accounts : function() {
return this.hasMany(Account, 'spaceId'); // spaceId is foreign key for Account table
}
});