Bookshelf.js:如何定义交叉关系?

时间:2013-08-13 09:49:20

标签: node.js relation bookshelf.js

我如何定义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/

1 个答案:

答案 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
        }
    });