Sequelize 关联错误无法读取未定义的属性“getTableName" [英] Sequelize Association Error Cannot read property 'getTableName' of undefined

查看:11
本文介绍了Sequelize 关联错误无法读取未定义的属性“getTableName"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将表关联到我的查询时,我遇到了一个错误消息,Unhandled reject TypeError: Cannot read property 'getTableName' of undefined.我在表之间有一对一的关系,但不确定这是否导致错误,或者它是否在我关联这两个表的其他地方.

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
            });
        })

    })

这是 models.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 供您用于 DiscoverySourceOrganization 模型.

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.

这篇关于Sequelize 关联错误无法读取未定义的属性“getTableName"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆