this is my first time using sequelize and I want to run a many to many migration. This is the setup of my models:
models/collection.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var Collection = sequelize.define('Collection', {
title: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
Collection.belongsToMany(Post, { through: CollectionPost, foreignKey: 'collection_id' });
}
}
});
return Collection;
};
and models/post.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var Post = sequelize.define('Post', {
title: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
Post.belongsToMany(Collection, { through: CollectionPost, foreignKey: 'post_id' });
}
}
});
return Post;
};
I understand that sequelize has sync
method that syncs the models with migrations.However I want to sync only once and hence I wrote this small script to that:
const models = require('../models');
function test() {
return models.sequelize.sync((err, response) => {
console.log(err);
console.log('.........');
console.log(response);
});
}
test();
However, this is not creating the CollectionPost
table. What am I missing ?