'Why am I getting a validation error? Nestjs, Sequelize , PostgressQL

I want to create a dependency where one User can have many InvestorCase, but one InvestorCase belongs to only one User. I need to have user_id field in InvestorCase.

User entity:

import { InvestorCase } from 'src/investor-case/entities/investor-case.entity';
import { ApiProperty } from '@nestjs/swagger';
import { Exclude } from 'class-transformer';
import {
  AllowNull,
  Column,
  DataType,
  Default,
  HasMany,
  IsIn,
  Model,
  Table,
} from 'sequelize-typescript';
import { UserRole, UserStatus } from 'src/shared/enums';
import { IUser } from 'src/shared/interfaces';

const userRoleValues = Object.values(UserRole);
const userStatusValues = Object.values(UserStatus);

@Table({ tableName: 'user' })
export class User extends Model<User, IUser> {
  @ApiProperty({ example: '1', description: 'User`s Id' })
  @Column({
    type: DataType.INTEGER,
    unique: true,
    autoIncrement: true,
    primaryKey: true,
  })
  public id: number;

  @ApiProperty({ example: '[email protected]', description: 'User`s Email' })
  @Column({
    type: DataType.STRING,
    allowNull: false,
  })
  public email: string;

  @ApiProperty({ example: 'password', description: 'User``s password' })
  @Column({
    type: DataType.STRING,
    allowNull: true,
  })
  @Exclude()
  public password: string;

  @ApiProperty({ example: 'Barak', description: 'User`s name' })
  @Column({
    type: DataType.STRING,
    allowNull: false,
  })
  public firstName: string;

  @ApiProperty({ example: 'Obama', description: 'User`s surname' })
  @Column({
    type: DataType.STRING,
    allowNull: false,
  })
  public lastName: string;

  @ApiProperty({ example: '3806799599432', description: 'User`s phone number' })
  @Column({
    type: DataType.STRING,
  })
  public phoneNumber: string;

  @ApiProperty({ example: 'verified', description: 'Account status' })
  @IsIn({
    args: [userStatusValues],
    msg: `User status must one of the following:
      ${userStatusValues.join(', ')}`,
  })
  @Default(UserStatus.UNVERIFIED)
  @Column
  public status: UserStatus;

  @ApiProperty({
    example: 'developer',
    description: 'User`s role',
    enum: UserRole,
  })
  @IsIn({
    args: [userRoleValues],
    msg: `User role must one of the following:
      ${userRoleValues.join(', ')}`,
  })
  @Default(UserRole.INVESTOR)
  @AllowNull(false)
  @Column
  public role: UserRole;

  @HasMany(() => InvestorCase)
  investorCases: InvestorCase[];
}

InvestorCare entity:

import { ApiProperty } from "@nestjs/swagger";
import { BelongsTo, Column, DataType, ForeignKey, IsIn, Model, PrimaryKey, Table } from "sequelize-typescript";
import { PaymentMethods } from 'src/shared/enums'
import { IInvestorCase } from 'src/shared/interfaces';
import { User } from "src/user/entities/user.entity";


const paymentMethods = Object.values(PaymentMethods);

@Table({ tableName: 'investor-case' })
export class InvestorCase extends Model<InvestorCase, IInvestorCase> {

    @ApiProperty({ example: '1', description: 'Unique ID' })
    @PrimaryKey
    @Column({ type: DataType.INTEGER, unique: true, autoIncrement: true })
    public id: number;


    @ApiProperty({ example: '10000', description: 'The amount the investor will deposit initially.' })
    @Column({ type: DataType.INTEGER, unique: true, allowNull: false, validate: { min: 1000 } })
    public initialPayment: number;


    @ApiProperty({ example: '1000', description: 'The amount that the investor will contribute monthly.' })
    @Column({ type: DataType.INTEGER, allowNull: true, validate: { min: 500 } })
    public monthlyPayment: number;


    @ApiProperty({
        example: 'true',
        description: 'The payment method by which the investments will be made.',
        enum: paymentMethods
    })
    @IsIn({
        args: [paymentMethods],
        msg: `The payment method must one of the following: ${paymentMethods.join(',')}`
    })
    @Column({ type: DataType.STRING, allowNull: false, defaultValue: PaymentMethods.Manually })
    public paymentMethod: string;


    @BelongsTo(() => User, {
        foreignKey: 'userId',
        as: 'UserId',
    })

    @ApiProperty({
        example: '1',
        description: 'Company representative user id',
    })
    @ForeignKey(() => User)
    @Column({ type: DataType.INTEGER })
    userId: number;
}

I try to create InvestorCase using this: { "initialPayment": 5000, "monthlyPayment": 1000, "paymentMethod": "Link a bank account", "userId": 2 }

[Nest] 244  - 05/10/2022, 10:30:26 AM   ERROR [ExceptionsHandler] Validation error
Error: 
    at Query.run (/app/node_modules/sequelize/src/dialects/postgres/query.js:76:25)
    at /app/node_modules/sequelize/src/sequelize.js:643:28
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at PostgresQueryInterface.insert (/app/node_modules/sequelize/src/dialects/abstract/query-interface.js:773:21)
    at InvestorCase.save (/app/node_modules/sequelize/src/model.js:4046:35)
    at Function.create (/app/node_modules/sequelize/src/model.js:2253:12)
    at InvestorCaseService.create (/app/src/investor-case/investor-case.service.ts:18:16)
    at InvestorCaseController.create (/app/src/investor-case/investor-case.controller.ts:18:16)
    at /app/node_modules/@nestjs/core/router/router-execution-context.js:46:28
    at /app/node_modules/@nestjs/core/router/router-proxy.js:9:17

But alwways got error:

[Nest] 244  - 05/10/2022, 10:30:26 AM   ERROR [ExceptionsHandler] Validation error
Error: 
    at Query.run (/app/node_modules/sequelize/src/dialects/postgres/query.js:76:25)
    at /app/node_modules/sequelize/src/sequelize.js:643:28
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at PostgresQueryInterface.insert (/app/node_modules/sequelize/src/dialects/abstract/query-interface.js:773:21)
    at InvestorCase.save (/app/node_modules/sequelize/src/model.js:4046:35)
    at Function.create (/app/node_modules/sequelize/src/model.js:2253:12)
    at InvestorCaseService.create (/app/src/investor-case/investor-case.service.ts:18:16)
    at InvestorCaseController.create (/app/src/investor-case/investor-case.controller.ts:18:16)
    at /app/node_modules/@nestjs/core/router/router-execution-context.js:46:28
    at /app/node_modules/@nestjs/core/router/router-proxy.js:9:17


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source