Sunday, 21 April 2019

'myTable' is not associated to Model

I'm working on an express sequelize project.

I've got an issue with sequelize.

I've got a simply schema like the documentation says:

class Task extends Model {}
Task.init({ title: Sequelize.STRING }, { sequelize, modelName: 'task' });
class User extends Model {}
User.init({ username: Sequelize.STRING }, { sequelize, modelName: 'user' });

User.hasMany(Task); // Will add userId to Task model
Task.belongsTo(User); // Will also add userId to Task model

But when I'm querying with include

sequelize.models['task'].findAll({include:[{all:true}]});

I'm facing to some issues:

The first :

this._getIncludedAssociation is not a function

Here is the github source origin code : https://github.com/sequelize/sequelize/blob/master/lib/model.js#L615

I just change this to Model to check.

This error is gone away, but a second (linked with the first, because I changed the behaviour), appears

User is not associated to Model

But It should say User is not associated to Task

(github of error: https://github.com/sequelize/sequelize/blob/master/lib/model.js#L711)

It's like the class Model wasn't instanciate...

What could be bad in my code ? Maybe the fact to call the model by sequelize.models[modelName] ?? I'm struggling with this for a long time...

Thanks.



from 'myTable' is not associated to Model

No comments:

Post a Comment