gpt4 book ai didi

node.js - 如何在 Sequelize 中联系

转载 作者:行者123 更新时间:2023-12-03 22:21:05 24 4
gpt4 key购买 nike

我试图将我的用户模型与帖子模型相关联,我想要做的是用户在创建帖子时将帖子 ID 保存在用户模型的“已发布”字段中,但我不能这样做
邮寄表:
enter image description here
'posteds' 不应该在那里,它应该在用户表中
我的关系:

Users.hasMany(Posts, {foreignKey: 'posteds'})
Posts.belongsTo(Users, {foreignKey : 'userId'})
模型用户:
import { DataTypes, UUIDV4, Optional, Model} from "sequelize"
import { connection } from "../database"
import { hash, genSalt, compare } from "bcryptjs";
import Posts from "./posts.model";

export const rolesEnum: string[] = ['ADMIN_ROLE', 'MODERATOR_ROLE','USER_ROLE']

interface UserAttributes{
id: number,
email: string,
password: string,
img_url: string,
role: 'ADMIN_ROLE' | 'MODERATOR_ROLE' | 'USER_ROLE',
created_at: Date,
updated_at?: Date
}


interface UserCreationAttributes extends Optional<UserAttributes, "id" | "created_at" | 'img_url'> {}
// We need to declare an interface for our model that is basically what our class would be
interface UserInstance
extends Model<UserAttributes, UserCreationAttributes>,
UserAttributes {}




const Users = connection.define<UserInstance>('users', {
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: UUIDV4,
unique: true,
allowNull: false
},
email: {
type: DataTypes.STRING,
unique: true,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
},
img_url: {
type: DataTypes.STRING,
defaultValue: process.env.DEFAULT_IMAGE || 'default.svg',
allowNull: false
},
role: {
type: DataTypes.STRING,
values: rolesEnum,
defaultValue: 'USER_ROLE',
allowNull: false
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false
},
updated_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,

}
},{
timestamps: false
})
export default Users
模特帖子:
import { DataTypes, Optional, Model,  UUIDV4} from "sequelize";
import {connection} from "../database";

interface PostAttributes {
id: number,
title: string,
description: string,
categorys?: Array<string>,
img_url: string,
created_at: Date,
updated_at?: Date,
userId?: string;
}

interface PostCreationAttributes extends Optional<PostAttributes, "id" | "created_at" |"img_url" | "categorys"> {}
// We need to declare an interface for our model that is basically what our class would be
interface PostInstance
extends Model<PostAttributes, PostCreationAttributes>,
PostAttributes {}


const Posts = connection.define<PostInstance>('posts', {
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: UUIDV4,
unique: true
},
title: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.STRING,
allowNull: false
},
categorys: {
type: DataTypes.ARRAY(DataTypes.ENUM('web-devlopment', 'recent', 'featured')),
values: ['web-devlopment', 'recent', 'featured'] //HELP TO SEND MESSAGES OF ERROR
},
img_url: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: process.env.DEFAULT_IMAGE || 'default.svg'
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false
},
updated_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
},{
timestamps: false
})


export default Posts

最佳答案

我相信问题出在 hasMany 定义上。因为每个用户可以有很多帖子 ID,所以您想定义关系但不希望用户表中有一个帖子列。
代替:
Users.hasMany(Posts, {foreignKey: 'posteds'})
你试过这个吗?
Users.hasMany(Posts, { as: 'posts'})
我引用了这个教程链接,也许它会有所帮助:
https://bezkoder.com/sequelize-associate-one-to-many/

关于node.js - 如何在 Sequelize 中联系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64000691/

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