TypeORM Cascade Update Issue

1.5k Views Asked by At

I am using TypeOrm with Postgres. I am having the below entity (minified)

    class A {
    
     @OneToMany()
     b:B
     @OneToMany()
     c:C
    }
    class B {
      @ManyToOne()
      a: A
    }
    
    class C {
      @OneToMany()
      d: D
      @OneToMany()
      e: E
      @ManyToOne()
      a: A
    }
    class D {
      @ManyToOne()
      c: C
    }
class E {
      @ManyToOne()
      c: C
    }

When I am trying to update entity A with a new items added to the collection c, d it is working fine and foreign keys are getting updated correctly. But when I am trying to update the same entity again after updating fields c,d, I can see that foreign keys are set as null for the entities C,D. I can see that 2 sets of update statements are getting fired. First one setting the foreign keys correctly and second one setting the keys to null. And this is not happening for entity B. All of oneToMany relations have { cascade: true, eager: true, onUpdate: "CASCADE", onDelete: "CASCADE" }. Any help to fix this issue will be helpful

1

There are 1 best solutions below

0
Anil Bhaskaran On

The issue is happening because class A is having an attribute, which is the foreign key of table c, referring to corresponding column in table A. We don't have to add the foreign key as a @Column in the child class.