Bookshelf.js: 一对多关系设置
Bookshelf.js: one-to-many relationship setup
我将 Bookshelf 用于 MySQL 的 ORM。我有端点和组织。每个端点都属于一个组织,一个组织有很多端点。我正在尝试获取端点及其关联组织的列表。这将返回以下错误:
"A valid target model must be defined for the endpoint belongsTo relation"
这是我的 fetchAll 请求:
Endpoint
.where('organization_id', req.user.attributes.organization_id)
.fetchAll({require: true,withRelated: ['settings', 'organization']})
.then((endpoints) => {
ReS(res, {
endpoints
}, 200);
})
这是我的端点模型:
'use strict';
const bookshelf = require('../config/bookshelf_instance');
const Organization = require('./organization');
const Settings = require('./endpoint_settings');
const Group = require('./endpoint_group');
module.exports = bookshelf.Model.extend({
tableName: 'endpoint',
organization () {
return this.belongsTo(Organization, 'id');
},
settings () {
return this.hasOne(Settings, 'id');
},
group () {
return this.belongsToMany(Group, 'map_endpoint_endpoint_group');
}
});
这是我的组织模型:
'use strict';
const bookshelf = require('../config/bookshelf_instance');
const Endpoint = require('./endpoint');
const User = require('./user');
const Group = require('./endpoint_group');
module.exports = bookshelf.Model.extend({
tableName: 'organization',
endpoints () {
return this.hasMany(Endpoint, 'organization_id');
},
users () {
return this.hasMany(User, 'organization_id');
},
groups () {
return this.hasMany(Group, 'organization_id');
}
});
您应该从端点模型的关系规范中删除 id
,因为它似乎是主键,而不是外键。默认情况下,外键也是 organization_id
,这就是您想要的。它应该匹配你在相反关系中的内容(在这种情况下为 hasMany
)。
根据文档,belongsTo
的第二个参数是:
ForeignKey in this model. By default, the foreignKey is assumed to be the singular form of the Target model's tableName, followed by _id.
目标模型的table名称为organization
,即外键默认为organization_id
。
我将 Bookshelf 用于 MySQL 的 ORM。我有端点和组织。每个端点都属于一个组织,一个组织有很多端点。我正在尝试获取端点及其关联组织的列表。这将返回以下错误:
"A valid target model must be defined for the endpoint belongsTo relation"
这是我的 fetchAll 请求:
Endpoint
.where('organization_id', req.user.attributes.organization_id)
.fetchAll({require: true,withRelated: ['settings', 'organization']})
.then((endpoints) => {
ReS(res, {
endpoints
}, 200);
})
这是我的端点模型:
'use strict';
const bookshelf = require('../config/bookshelf_instance');
const Organization = require('./organization');
const Settings = require('./endpoint_settings');
const Group = require('./endpoint_group');
module.exports = bookshelf.Model.extend({
tableName: 'endpoint',
organization () {
return this.belongsTo(Organization, 'id');
},
settings () {
return this.hasOne(Settings, 'id');
},
group () {
return this.belongsToMany(Group, 'map_endpoint_endpoint_group');
}
});
这是我的组织模型:
'use strict';
const bookshelf = require('../config/bookshelf_instance');
const Endpoint = require('./endpoint');
const User = require('./user');
const Group = require('./endpoint_group');
module.exports = bookshelf.Model.extend({
tableName: 'organization',
endpoints () {
return this.hasMany(Endpoint, 'organization_id');
},
users () {
return this.hasMany(User, 'organization_id');
},
groups () {
return this.hasMany(Group, 'organization_id');
}
});
您应该从端点模型的关系规范中删除 id
,因为它似乎是主键,而不是外键。默认情况下,外键也是 organization_id
,这就是您想要的。它应该匹配你在相反关系中的内容(在这种情况下为 hasMany
)。
根据文档,belongsTo
的第二个参数是:
ForeignKey in this model. By default, the foreignKey is assumed to be the singular form of the Target model's tableName, followed by _id.
目标模型的table名称为organization
,即外键默认为organization_id
。