这是我第一次使用 sequelize,我想运行多对多迁移。这是我的模型的设置:
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;
};
和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;
};
我知道 sequelize 具有sync
将模型与迁移同步的方法。但是我只想同步一次,因此我写了这个小脚本:
const models = require('../models');
function test() {
return models.sequelize.sync((err, response) => {
console.log(err);
console.log('.........');
console.log(response);
});
}
test();
但是,这不是创建CollectionPost
表。我错过了什么?