Issue with self referencing many to many relationship in typeorm

32 Views Asked by At

I have the following Entity:

import { MinLength } from "class-validator";
import { BeforeInsert, BeforeUpdate, Column, CreateDateColumn, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";
import { checkInput } from "../utils/checkInput";
import { Role } from "./Role";

//{statusName,statusDate,nextStatuses:[],statusSender:User}
@Entity()
export class Status{
    @PrimaryGeneratedColumn()
    _id:number

    @Column({
        type:'varchar',
        unique:true,
        collation:'SQL_Latin1_General_CP1_CI_AS'
    })
    @MinLength(1)
    statusName:string

    

    @ManyToMany(type => Status,{cascade:false})
    @JoinTable()
    nextStatuses: Status[];
    

    @ManyToOne(() => Role, (role:Role) => role._id)
    statusType:Role

    @CreateDateColumn()
    createdAt:Date
    
    @UpdateDateColumn()
    updatedAt:Date

    @BeforeInsert()
    @BeforeUpdate()
        updateStatus(){
                checkInput(this);
            }
}

I am trying to create a self-referencing many-to-many relationship nextStatuses but I constantly receive an error:

RequestError: Introducing FOREIGN KEY constraint 'FK_caa566520c21ac715763f85e5ff' on table 'status_next_statuses_status' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

I tried to run with cascade:false:

@ManyToMany(type => Status,{cascade:false})
@JoinTable()
nextStatuses: Status[];

But this does help - I continue to receive the same error

I am using Typescript and SQL Server

0

There are 0 best solutions below