续集关联错误无法读取未定义的属性“getTableName" [英] Sequelize Association Error Cannot read property 'getTableName' of undefined
问题描述
我遇到了一个问题,当我尝试将一个表关联到我的查询中时,我收到一条错误消息,未处理的拒绝类型错误:无法读取未定义的属性 'getTableName'
.我在表之间建立了一对一的关系,但不确定这是导致错误的原因,还是我将两个表关联的其他地方.
I am running into an issue where I get an error message, Unhandled rejection TypeError: Cannot read property 'getTableName' of undefined
when I try to associate a table into my query. I have a one-to-one relationship between the tables and am not sure if this is causing the error or if it is somewhere else where I am associating the two tables.
这是我的查询:
appRoutes.route('/settings')
.get(function(req, res, organization){
models.DiscoverySource.findAll({
where: {
organizationId: req.user.organizationId
},
include: [{
model: models.Organization, through: { attributes: ['organizationName', 'admin', 'discoverySource']}
}]
}).then(function(organization, discoverySource){
res.render('pages/app/settings.hbs',{
user: req.user,
organization: organization,
discoverySource: discoverySource
});
})
})
这是模型.DiscoverySource 模型:
Here is the models.DiscoverySource model:
module.exports = function(sequelize, DataTypes) {
var DiscoverySource = sequelize.define('discovery_source', {
discoverySourceId: {
type: DataTypes.INTEGER,
field: 'discovery_source_id',
autoIncrement: true,
primaryKey: true
},
discoverySource: {
type: DataTypes.STRING,
field: 'discovery_source_name'
},
organizationId: {
type: DataTypes.TEXT,
field: 'organization_id'
},
},{
freezeTableName: true,
classMethods: {
associate: function(db) {
DiscoverySource.belongsTo(db.Organization, {foreignKey: 'organization_id'});
},
},
});
return DiscoverySource;
}
这是我的模型.组织模型:
Here is my models.Organization model:
module.exports = function(sequelize, DataTypes) {
var Organization = sequelize.define('organization', {
organizationId: {
type: DataTypes.INTEGER,
field: 'organization_id',
autoIncrement: true,
primaryKey: true
},
organizationName: {
type: DataTypes.STRING,
field: 'organization_name'
},
admin: DataTypes.STRING
},{
freezeTableName: true,
classMethods: {
associate: function(db) {
Organization.belongsToMany(db.User, { through: 'member', foreignKey: 'user_id' });
},
}
});
return Organization;
}
推荐答案
您需要将查询重写为:
models.DiscoverySource.findAll({
attributes: ['discoverySource'],
where: {
organizationId: req.user.organizationId
},
include: [{
model: models.Organization,
attributes: ['organizationName', 'admin']
}]
})
根据 Sequelize 文档:
According to the Sequelize documentation:
[options.attributes] - 要选择的属性列表,或具有包含和排除键的对象.
[options.attributes] - A list of the attributes that you want to select, or an object with include and exclude keys.
[options.include[].attributes] - 要从子模型中选择的属性列表.
[options.include[].attributes] - A list of attributes to select from the child model.
[options.include[].through.where] - 过滤belongsToMany 关系的连接模型.
[options.include[].through.where] - Filter on the join model for belongsToMany relations.
[options.include[].through.attributes] - 要从belongsToMany关系的连接模型中选择的属性列表.
[options.include[].through.attributes] - A list of attributes to select from the join model for belongsToMany relations.
所以,[options.include[].through]
只能用于 Belongs-To-Many
关联而不是 Belong-To
用于 DiscoverySource
和 Organization
模型.
So, [options.include[].through]
can only be used in case of Belongs-To-Many
association rather than Belong-To
used by you for DiscoverySource
and Organization
models.
这篇关于续集关联错误无法读取未定义的属性“getTableName"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!