typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

Child entities with orphanedRowAction with foreign keys as composite primary key are deleted when updated

Open Metheny80 opened this issue 5 years ago • 11 comments

Issue Description

Expected Behavior

Child entities should exist after the save operation.

Actual Behavior

Child entities are updated then deleted when calling save.

Steps to Reproduce

  1. Define an entity Parent with OneToMany relationship to another entity Child
  2. Entity Child has a composite primary key: parent (Parent) id + another entity (OtherEntity) id
  3. Save an object of type Parent with at least 1 child of type Child.
  4. Make changes in one of the Child objects and save the parent again. --> Children will be updated and then deleted (in the end there are no children in the DB), instead of being saved to the DB.
@Entity({ name: 'parents' })
export class Parent {
    @PrimaryGeneratedColumn({ unsigned: true })
    id!: number;

    @OneToMany(() => Child, child => child .parent, { cascade: true })
    children!: Child[];
}


@Entity({ name: 'children' })
export class Child {
    @OneToOne(() => OtherEntity)
    @JoinColumn({ name: 'otherEntityId', referencedColumnName: "id" })
    otherEntity!: OtherEntity;

    @PrimaryColumn({ name: "otherEntityId" })
    otherEntityId!: number;  

    @ManyToOne(() => Parent, parent => parent.children, { orphanedRowAction: 'delete' })
    @JoinColumn({ name: "parentId", referencedColumnName: "id" })
    parent!: Parent;

    @PrimaryColumn({ name: "parentId" })
    parentId!: number;    

    @Column({ name: 'order' })
    order!: number; 
}

@Entity({ name: 'otherEntities' })
export class OtherEntity {
    @PrimaryGeneratedColumn({ unsigned: true })
    id!: number;
}

My Environment

| Dependency | Version |

| Operating System AWS Lambda | Node.js version 14 | Typescript version 4.3.4 | TypeORM version 0.2.34

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql yes
nativescript no
oracle no
postgres no
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✅ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Metheny80 avatar Sep 11 '21 16:09 Metheny80

because you set orphanedRowAction: delete so it was deleted by the relation

ganshiqingyuan avatar Sep 14 '21 03:09 ganshiqingyuan

because you set orphanedRowAction: delete so it was deleted by the relation

I didn't delete any child, only updated the child objects, see the scenario. In the end there were no children in the DB although I only updated the children. That doesn't make any sense.

Metheny80 avatar Sep 14 '21 05:09 Metheny80

because you set orphanedRowAction: delete so it was deleted by the relation

I didn't delete any child, only updated the child objects, see the scenario. In the end there were no children in the DB although I only updated the children. That doesn't make any sense.

if your save a parent entity includes child and value is [] , then all old child will be remove by your orphanedRowAction , unless there is no child attribute in parent entity to be saved

ganshiqingyuan avatar Sep 14 '21 06:09 ganshiqingyuan

because you set orphanedRowAction: delete so it was deleted by the relation

I didn't delete any child, only updated the child objects, see the scenario. In the end there were no children in the DB although I only updated the children. That doesn't make any sense.

if your save a parent entity includes child and value is [] , then all old child will be remove by your orphanedRowAction , unless there is no child attribute in parent entity to be saved

But the value is not [], the Parent object still contains the same amount of children when saving.

Metheny80 avatar Sep 14 '21 06:09 Metheny80

does the children has primaryId in it ? otherwise typeorm will think these are new child and still remove your old children

ganshiqingyuan avatar Sep 15 '21 02:09 ganshiqingyuan

does the children has primaryId in it ? otherwise typeorm will think these are new child and still remove your old children

Please see the code in the opening for additional questions, all the information is there. The end result should be that the parent has children in the DB.

Metheny80 avatar Sep 15 '21 06:09 Metheny80

does the children has primaryId in it ? otherwise typeorm will think these are new child and still remove your old children

Please see the code in the opening for additional questions, all the information is there. The end result should be that the parent has children in the DB.

this depend on how you saved parent, if you save

{
 child:[{
    otherEntity: xxx
  }]
}

no otherEntityId in saved child.. your old child will be removed

ganshiqingyuan avatar Sep 15 '21 10:09 ganshiqingyuan

does the children has primaryId in it ? otherwise typeorm will think these are new child and still remove your old children

Please see the code in the opening for additional questions, all the information is there. The end result should be that the parent has children in the DB.

this depend on how you saved parent, if you save

{
 child:[{
    otherEntity: xxx
  }]
}

no otherEntityId in saved child.. your old child will be removed

There is OtherEntity in the children. The fact is that after the first save the child was saved to the DB. After the second save the all children were removed from DB. The first & second saved object hierarchy are completely the same, with only non-important changes (e.g. changed the order property in Child). After the first the child was inserted, and after the second save it was removed. My feeling is that there is a bug in typeorm that first handles the new children and then the old ones (i.e. instead of deleting the old ones and then inserting the new ones, it first inserts/updates and then removes the old ones by key, which is the same key as the new ones).

Metheny80 avatar Sep 15 '21 11:09 Metheny80

if the child primaryId is the same..typeorm will update it.. you can use logging: true in config to check up all the output executed sql

ganshiqingyuan avatar Sep 16 '21 02:09 ganshiqingyuan

I saw in the logs that the children were updated and then deleted in the end.

Metheny80 avatar Sep 16 '21 05:09 Metheny80

Just found this, I think it is related to the bug report that I just posted: https://github.com/typeorm/typeorm/issues/8212

@Metheny80 Can you add a console logging to typeorm source /persistence/SubjectExecutor.js at line 184, just where broadcastBeforeEventsForAll starts?

		console.log('this.updateSubjects length',this.updateSubjects.length)
		console.log('this.insertSubjects length',this.insertSubjects.length)
		console.log('this.removeSubjects length',this.removeSubjects.length)
		console.log('this.recoverSubjects length',this.recoverSubjects.length)
		console.log('this.softRemoveSubjects length',this.softRemoveSubjects.length)

save, recompile/restart your app and see what it is logging. For me, it will always have softRemoveSubjects being generated for related items, due to the bug https://github.com/typeorm/typeorm/issues/8212

jbjhjm avatar Sep 23 '21 16:09 jbjhjm