gpt4 book ai didi

javascript - Sequelize 关联不与模型定义同步

转载 作者:行者123 更新时间:2023-12-03 22:36:55 26 4
gpt4 key购买 nike

'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;
};

还有我的评论评分
'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;
};

但是,当我使用 findOrCreate 方法时,它只执行 INSERT INTO "article_comments_user_ratings" ("id","rating","createdAt","updatedAt"). 这显然是失败的,因为在数据库中,我还为 article_comments_user_ratings 表添加了 user_id 和 comment_id 列。

这没有任何意义,因为使用 sync() 函数,在迁移到迁移之前,它正在工作。

我不知道该怎么办?

最佳答案

我通过在定义模型后定义关联来解决这个问题。

例子:

const User = UserModel(sequelize, Sequelize)
const Comment = CommentsModel(sequelize, Sequelize)

User.hasMany(UserCommentRating,{foreignKey:'user_id'})
Comment.hasMany(UserCommentRating,{foreignKey:'comment_id'})

模型内部的关联似乎已被弃用。

关于javascript - Sequelize 关联不与模型定义同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58244656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com