ava*_*sin 1 relation node.js bookshelf.js
我如何定义hasMany Space - > Accounts关系?
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);
},
});
Run Code Online (Sandbox Code Playgroud)
定义这个的正确方法是什么?
PS bookhelf js library没有标签:http://bookshelfjs.org/
根据文件,这应该工作:
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
}
});
Run Code Online (Sandbox Code Playgroud)