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: ‘posteds’ shouldn’t be there, it should be in the users table […]
Categories
How to relate in sequelize
- Post author By Full Stack
- Post date September 24, 2020
- No Comments on How to relate in sequelize
- Tags 'MODERATOR_ROLE', 'USER_ROLE'] interface UserAttributes{ id: number, "id" | "created_at" | 'img_url'> {} // We need to declare an interface for our model that is basically what our class would be interface User, "password": "string", { id: { type: DataTypes.UUID, {foreignKey : 'userId'}) Model User: import { DataTypes, {foreignKey: 'posteds'}) Posts.belongsTo(Users, allowNull: false, but I can't do it Post Table: 'posteds' shouldn't be there, compare } from "bcryptjs"; import Posts from "./posts.model"; export const rolesEnum: string[] = ['ADMIN_ROLE', created_at DATE, created_at: { type: DataTypes.DATE, defaultValue: 'USER_ROLE', defaultValue: DataTypes.NOW, defaultValue: process.env.DEFAULT_IMAGE || 'default.svg', defaultValue: UUIDV4, email: { type: DataTypes.STRING, email: String, genSalt, I am trying to relate my Users model with the Posts model, img_url: { type: DataTypes.STRING, img_url: string, it should be in the users table My relationships: Users.hasMany(Posts, Model} from "sequelize" import { connection } from "../database" import { hash, optional), password: { type: DataTypes.STRING, primaryKey: true, role: 'ADMIN_ROLE' | 'MODERATOR_ROLE' | 'USER_ROLE', role: { type: DataTypes.STRING, unique = true, updated_at: { type: DataTypes.DATE, updated_at?: Date } interface UserCreationAttributes extends Optional<UserAttributes, UserAttributes {} const Users = connection.define('users', UserCreationAttributes>, UUIDV4, values: rolesEnum, 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