gpt4 book ai didi

node.js - SequelizeJS 无法读取未定义的属性 'getTableName'

转载 作者:太空宇宙 更新时间:2023-11-03 23:32:49 26 4
gpt4 key购买 nike

我遇到了一个问题,我试图关联两个具有一对多关系的模型。由于某种原因,尽管引用了关系,此查询仍会引发错误。

这是我的错误消息:

Unhandled rejection TypeError: Cannot read property 'getTableName' of undefined
at generateJoinQueries (/Users/user/Desktop/Projects/node/project/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1181:43)

这是路线:

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',{
organization: organization,
discoverySource: 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;
}

组织:

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.hasMany(db.DiscoverySource, {foreignKey: 'organization_id'});
},
}
});
return Organization;
}

最佳答案

看来,这是 Sequelize Association Error Cannot read property 'getTableName' of undefined 的重复项.

但是,您需要重写查询,如下所示:

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 关联的情况下使用,而不是在您为 DiscoverySourceOrganization 模型使用的 Belong-To 情况下使用。

关于node.js - SequelizeJS 无法读取未定义的属性 'getTableName',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36121767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com