Sequelize 关联错误无法读取未定义的 属性 'getTableName'
Sequelize Association Error Cannot read property 'getTableName' of undefined
我 运行 遇到一个问题,当我尝试将 table 关联到我的查询时 Unhandled rejection TypeError: Cannot read property 'getTableName' of undefined
收到一条错误消息。我在 table 之间有一对一的关系,我不确定这是否导致了错误,或者它是否在我关联两个 table 的其他地方。
这是我的查询:
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
});
})
})
这里是 models.DiscoverySource 模型:
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;
}
这是我的 models.Organization 模型:
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 文档:
[options.attributes] - A list of the attributes that you want to select, or an object with include and exclude keys.
[options.include[].attributes] - A list of attributes to select from the child model.
[options.include[].through.where] - Filter on the join model for belongsToMany relations.
[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
模型。
我遇到了同样的问题。我的续集版本 5.22.3
。
如果您使用相同的版本,请对您的查询进行以下更改:
models.DiscoverySource.findAll({
attributes: ['discoverySource'],
where: {
organizationId: req.user.organizationId
},
include: {
model: models.Organization,
//rest of the code, if any
}
})
include 不再需要是数组。
在我的例子中删除
through: { attributes: [] }
解决了问题。
我 运行 遇到一个问题,当我尝试将 table 关联到我的查询时 Unhandled rejection TypeError: Cannot read property 'getTableName' of undefined
收到一条错误消息。我在 table 之间有一对一的关系,我不确定这是否导致了错误,或者它是否在我关联两个 table 的其他地方。
这是我的查询:
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
});
})
})
这里是 models.DiscoverySource 模型:
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;
}
这是我的 models.Organization 模型:
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 文档:
[options.attributes] - A list of the attributes that you want to select, or an object with include and exclude keys.
[options.include[].attributes] - A list of attributes to select from the child model.
[options.include[].through.where] - Filter on the join model for belongsToMany relations.
[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
模型。
我遇到了同样的问题。我的续集版本 5.22.3
。
如果您使用相同的版本,请对您的查询进行以下更改:
models.DiscoverySource.findAll({
attributes: ['discoverySource'],
where: {
organizationId: req.user.organizationId
},
include: {
model: models.Organization,
//rest of the code, if any
}
})
include 不再需要是数组。
在我的例子中删除
through: { attributes: [] }
解决了问题。