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;
}
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.
In my case removing
through: { attributes: [] }
solved the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With