I have recently switched from "normal" sequelize to migrations with sequelize-cli. Using the cli worked fine so far. But I am unsure how to use the models that are generated. In particular it seems that the way I do it, I have issues with the associations. They are properly created in the database, but in the code I get errors like Upvote is not associated to Video
I have created the models with the cli and tried to import and use them.
This is a snippet of the video
model generated by the cli:
module.exports = (sequelize, DataTypes) => {
const Video = sequelize.define('Video', {
userid: DataTypes.STRING,
...
}, {});
Video.associate = function(models) {
Video.hasMany(models.Upvote, { foreignKey: 'videoid', sourceKey: 'id' });
};
return Video;
};
Here comes a snippet of the migration:
let videos = function (queryInterface, Sequelize) {
return queryInterface.createTable('Videos', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userid: {
type: Sequelize.STRING,
references: {
model: 'Users',
key: 'id'
},
},
...
});
};
This is the way I import the models:
const Video = require('../models/video')(dbConnector.sequelize, Sequelize.DataTypes);
I find it a little weird that I need to pass sequelize
and DataTypes
, since this was not necessary when I built the models myself. Is this the right way of importing them?
There is no error when I import them, and I can query them like Videos.findAll()
but the associations don't work. And I am not sure how to make them work, do I need to call the Video.associate(models)
function myself? That seems weird, too.
I must be doing something wrong in the way I use those models, please let me know what to do differently.