Sequelize Association Error无法读取属性' getTableName'未定义的

时间:2016-03-13 19:08:39

标签: 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
            });
        })

    })

这是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;
}

这是我的模特。组织模式:

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

3 个答案:

答案 0 :(得分:3)

您需要将查询重写为:

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

根据Sequelize文档:

  

[options.attributes] - 要选择的属性列表,或包含include和exclude键的对象。

     

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

     

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

     

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

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

答案 1 :(得分:0)

我遇到了同样的问题。我的续集版本 5.22.3。 如果您使用相同的版本,请对您的查询进行以下更改:

models.DiscoverySource.findAll({
    attributes: ['discoverySource'],
    where: { 
        organizationId: req.user.organizationId
    },
    include: {
        model: models.Organization,
        //rest of the code, if any
    }
})

include 不再需要是一个数组。

答案 2 :(得分:0)

在我的情况下删除

through: { attributes: [] }

解决了问题。