Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize Association Error 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.

Here is my query:

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

    })

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;
}
like image 515
cphill Avatar asked Mar 13 '16 19:03

cphill


2 Answers

You need to rewrite your query to look like:

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

According to the Sequelize documentation:

[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.

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.

like image 118
Alex M Avatar answered Nov 19 '22 13:11

Alex M


In my case removing

through: { attributes: [] }

solved the problem.

like image 31
Khushal Vyas Avatar answered Nov 19 '22 12:11

Khushal Vyas