续集关联错误无法读取未定义的属性“getTableName”

cph*_*ill 10 node.js sequelize.js

Unhandled rejection TypeError: Cannot read property 'getTableName' of undefined当我尝试将表关联到我的查询时,我遇到了收到错误消息的问题。我在表之间有一对一的关系,但不确定这是导致错误还是在其他地方我将两个表相关联。

这是我的查询:

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

    })
Run Code Online (Sandbox Code Playgroud)

这是 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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的模型。组织模型:

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;
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*x M 15

您需要将查询重写为:

models.DiscoverySource.findAll({
    attributes: ['discoverySource'],
    where: { 
        organizationId: req.user.organizationId
    },
    include: [{
        model: models.Organization,
        attributes: ['organizationName', 'admin']
    }]
})
Run Code Online (Sandbox Code Playgroud)

根据 Sequelize 文档:

[options.attributes] - 要选择的属性列表,或具有包含和排除键的对象。

[options.include[].attributes] - 要从子模型中选择的属性列表。

[options.include[].through.where] - 过滤belongsToMany 关系的连接模型。

[options.include[].through.attributes] - 要从belongsToMany 关系的连接模型中选择的属性列表。

因此,[options.include[].through]只能在Belongs-To-Many关联的情况下使用,而不能Belong-To由您用于DiscoverySourceOrganization模型。


小智 6

就我而言,删除

through: { attributes: [] }
Run Code Online (Sandbox Code Playgroud)

解决了问题。

  • 也为我工作,谢谢伙计 PS:Sequelize 缺乏文档,很多文档 (2认同)