Error: <Table> is not associated to <Table>!
See original GitHub issueHello,
I am trying to emulate this query:
var sql = squel.select()
.from('tags')
.field('tags.tagName')
.field('count(*) as count')
.join('screencastTags', null, 'screencastTags.tagName = tags.tagName')
.join('screencasts', null, 'screencasts.screencastId = screencastTags.screencastId')
.where('screencasts.approved = 1')
.group('tags.tagName')
.order('count', false)
.limit(20)
.toString();
connection.queryAsync(sql).spread(function(tags) {
})
Here is what I tried in Sequelize:
models.ScreencastTag.findAll({
group: ['tagName'],
attributes: ['tagName', [Sequelize.fn('count', 'tagName'), 'count']],
limit: 20,
include: [{
model: models.Screencast,
where: { approved: true }
}]
}).then(function (tags) {
res.json({tags: tags});
});
But I get this error:
Unhandled rejection Error: Screencast is not associated to ScreencastTag!
at validateIncludedElement (c:\Users\alexb\Documents\Code\communitycasts.co\source\server\node_modules\sequelize\lib\model.js:559:11)
at c:\Users\alexb\Documents\Code\communitycasts.co\source\server\node_modules\sequelize\lib\model.js:445:29
at Array.map (native)
at validateIncludedElements (c:\Users\alexb\Documents\Code\communitycasts.co\source\server\node_modules\sequelize\lib\model.js:441:37)
at null.<anonymous> (c:\Users\alexb\Documents\Code\communitycasts.co\source\server\node_modules\sequelize\lib\model.js:1315:34)
at tryCatcher (c:\Users\alexb\Documents\Code\communitycasts.co\source\server\node_modules\bluebird\js\main\util.js:26:23)
at Promise._settlePromiseFromHandler (c:\Users\alexb\Documents\Code\communitycasts.co\source\server\node_modules\bluebird\js\main\promise.js:503:31)
at Promise._settlePromiseAt (c:\Users\alexb\Documents\Code\communitycasts.co\source\server\node_modules\bluebird\js\main\promise.js:577:18)
at Async._drainQueue (c:\Users\alexb\Documents\Code\communitycasts.co\source\server\node_modules\bluebird\js\main\async.js:128:12)
at Async._drainQueues (c:\Users\alexb\Documents\Code\communitycasts.co\source\server\node_modules\bluebird\js\main\async.js:133:10)
at Immediate.Async.drainQueues [as _onImmediate] (c:\Users\alexb\Documents\Code\communitycasts.co\source\server\node_modules\bluebird\js\main\async.js:15:14)
at processImmediate [as _immediateCallback] (timers.js:367:17)
Am I doing something wrong or is this a bug?
Here are my model definitions:
screencastTag.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var ScreencastTag = sequelize.define('ScreencastTag', {
screencastId: {
type: DataTypes.STRING,
primaryKey: true
},
tagName: {
type: DataTypes.STRING,
primaryKey: true
}
}, {
timestamps: false
});
return ScreencastTag;
};
tag.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var Tag = sequelize.define('Tag', {
tagName: {
type: DataTypes.STRING,
primaryKey: true
}
}, {
timestamps: false,
classMethods: {
associate: function (models) {
Tag.belongsToMany(models.Screencast, { through: models.ScreencastTag });
}
}
});
return Tag;
};
screencast.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var Screencast = sequelize.define('Screencast', {
screencastId: {
type: DataTypes.STRING,
primaryKey: true
},
title: {
type: DataTypes.STRING,
},
durationInSeconds: {
type: DataTypes.INTEGER(11),
},
description: {
type: DataTypes.STRING,
},
submissionDate: {
type: DataTypes.DATE,
},
referralCount: {
type: DataTypes.INTEGER(11),
},
channelId: {
type: DataTypes.STRING,
},
approved: {
type: DataTypes.BOOLEAN,
},
featured: {
type: DataTypes.BOOLEAN,
}
}, {
timestamps: false,
classMethods: {
associate: function (models) {
Screencast.belongsToMany(models.Tag, {through: models.ScreencastTag});
}
}
});
return Screencast;
};
index.js
'use strict';
var config = require('config');
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var db = { };
var sequelize = new Sequelize('demo', 'root', config.databasePassword, {
host: 'localhost'
});
fs.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf('.') !== 0) && (file !== 'index.js');
})
.forEach(function(file) {
var model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName) {
if ('associate' in db[modelName]) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
sequelize.sync({force: true});
module.exports = db;
Thank you.
Issue Analytics
- State:
- Created 8 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
sequelize table is not associated to user - Stack Overflow
But when I access to /user/test, it throw error like this -> SequelizeEagerLoadingError: follow is not associated to user!
Read more >[Solved]-sequelize table is not associated to user-sequelize.js
Coding example for the question sequelize table is not associated to user-sequelize.js. ... The include part is not correct. You should include the...
Read more >Advanced M:N Associations | Sequelize
You probably noticed that the User_Profiles table does not have an id field. ... constructed by Sequelize based on the name of the...
Read more >2 Server Error Message Reference - MySQL :: Developer Zone
InnoDB reports this error when the table from the InnoDB data files cannot be found. ... Message: Table '%s' has no index like...
Read more >Errors when open or make a reference to a linked table - Office
You may receive this error message if Access cannot find the table or the query in the database, if a link points to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@alexbooker It’s simply a legacy thing, the codebase has never done it previously and at this point we’re a bit worried about introducing magic that might break existing codebases in weird ways.
belongsToManyor the previous2 x hasManyhas always just maintained relationsships to and from target using the join model as an intermediary, it doesn’t set up relations to the join model.@mickhansen I would love to help out but I do not understand why it is necessary myself, I am afraid.