Thursday 24 September 2020

How to relate one to many sequelize

I am trying to relate my Users model with the Posts model, what I want to do is that the user when creating a post saves the post id in the 'posteds' field of the user model, but I can't do it

Post Table:

enter image description here

'posteds' shouldn't be there, it should be in the users table

My relationships:

Users.hasMany(Posts, {foreignKey: 'posteds'})
Posts.belongsTo(Users, {foreignKey : 'userId'})

Model User:

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

Model Posts:

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


from How to relate one to many sequelize

No comments:

Post a Comment