Javascript 使用bookshelf.js多态关系进行反向关系查找
我使用多态关系来允许Javascript 使用bookshelf.js多态关系进行反向关系查找,javascript,node.js,orm,bookshelf.js,Javascript,Node.js,Orm,Bookshelf.js,我使用多态关系来允许帐户与两种不同的用户类型相关联:客户和员工。我对模型的定义如下: var Customer = bookshelf.Model.extend({ tableName: 'customer', accounts: function() { return this.morphMany(Account, 'concernable'); } }); var Staff = bookshelf.Model.extend({ tableName: 'staff'
帐户
与两种不同的用户类型相关联:客户
和员工
。我对模型的定义如下:
var Customer = bookshelf.Model.extend({
tableName: 'customer',
accounts: function() {
return this.morphMany(Account, 'concernable');
}
});
var Staff = bookshelf.Model.extend({
tableName: 'staff',
accounts: function() {
return this.morphMany(Account, 'concernable');
}
});
var Account = bookshelf.Model.extend({
tableName: 'account',
concernable: function () {
return this.morphTo('concernable', 'Staff', 'Customer');
}
});
我的数据库是Postgres,我用于帐户的模式是:
id (int)
business_id (int)
concernable_id (int)
concernable_type (string)
我试图做的是获取属于特定业务的所有帐户,并返回与这些帐户关联的客户。这与我在书架示例中看到的标准关系查找相反。以下是我在没有运气的情况下尝试过的:
Account.where({
business_id: filter.businessId,
concernable_type: 'customer'
})
.fetchAll({
withRelated: ['concernable']
})
.then((response) => {
return reply.jsonapi(response, 'customer');
});
我收到以下错误:未找到目标多态模型
。如果我将withRelated
更改为customer
,则错误是在型号上找不到customer
我想做的事可能吗
谢谢 你能解决这个问题吗?我也面临同样的问题。你能解决这个问题吗?我也面临同样的问题。