Reputation: 9726
How can i define hasMany Space -> Accounts relation?
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);
},
});
What is the correct way to define this?
P.S. There is no tag for bookshelf js library: http://bookshelfjs.org/
Upvotes: 1
Views: 3675
Reputation: 692
According to Docs, this should work:
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
}
});
Upvotes: 8