Javascript Sequelize关联未与模型定义同步
以及我的评论评级Javascript Sequelize关联未与模型定义同步,javascript,node.js,sequelize.js,sequelize-cli,Javascript,Node.js,Sequelize.js,Sequelize Cli,以及我的评论评级 'use strict'; module.exports = (sequelize, type) => { const article_comment = sequelize.define('article_comments', { // attributes id: { type: type.INTEGER, primaryKey: true, autoIncrement: true }, posi
'use strict';
module.exports = (sequelize, type) => {
const article_comment = sequelize.define('article_comments', {
// attributes
id: {
type: type.INTEGER,
primaryKey: true,
autoIncrement: true
},
positive_rating:{
type: type.INTEGER,
allowNull: false
},
negative_rating:{
type: type.INTEGER,
allowNull: false
},
comment:{
type: type.STRING,
allowNull: false
},
updatedAt:{
type: type.STRING,
allowNull: false
},
createdAt:{
type: type.STRING,
allowNull: false
},
}, {});
article_comment.associate = function(models) {
// associations can be defined here
article_comment.hasMany(models.article_comments_user_ratings,{foreignKey:'comment_id'});
article_comment.hasMany(models.article_replies,{foreignKey:'comment_id'});
};
return article_comment;
};
然而,当我使用findOrCreate方法时,它只会插入“article\u comments\u user\u ratings”(“id”、“rating”、“createdAt”、“updatedAt”)。
这显然是失败的,因为在数据库中,我还为article\u comments\u user\u ratings表添加了user\u id和comment\u id列
这没有任何意义,因为使用sync()函数,在迁移之前,它可以正常工作
我不知道该怎么办?我在定义模型后通过定义关联解决了这个问题 例如:
'use strict';
module.exports = (sequelize, type) => {
const article_comments_user_ratings = sequelize.define('article_comments_user_ratings', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: type.INTEGER
},
rating:{
type: type.BOOLEAN,
allowNull: false
},
createdAt: {
allowNull: false,
type: type.DATE
},
updatedAt: {
allowNull: false,
type: type.DATE
}
}, {});
article_comments_user_ratings.associate = function(models) {
// associations can be defined here
article_comments_user_ratings.belongsTo(models.article_comments)
};
return article_comments_user_ratings;
};
模型中的关联似乎已被弃用
const User = UserModel(sequelize, Sequelize)
const Comment = CommentsModel(sequelize, Sequelize)
User.hasMany(UserCommentRating,{foreignKey:'user_id'})
Comment.hasMany(UserCommentRating,{foreignKey:'comment_id'})