【问题标题】:How to relate in sequelize如何在续集中关联
【发布时间】:2020-09-21 22:22:38
【问题描述】:

我正在尝试将我的用户模型与帖子模型相关联,我想做的是用户在创建帖子时将帖子 ID 保存在用户模型的“已发布”字段中,但我做不到它

发布表:

'posteds' 不应该在那里,它应该在 users 表中

我的人际关系:

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

【问题讨论】:

    标签: node.js database postgresql sequelize.js


    【解决方案1】:

    我认为问题在于 hasMany 定义。因为每个用户可以有多个帖子 ID,所以您希望定义关系,但不希望 users 表中有一个帖子列。

    而不是: Users.hasMany(Posts, {foreignKey: 'posteds'})

    你试过吗? Users.hasMany(Posts, { as: 'posts'})

    我参考了这个教程链接,也许它会有所帮助: https://bezkoder.com/sequelize-associate-one-to-many/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-30
      • 2019-02-12
      • 1970-01-01
      • 2019-04-05
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多