Child entities with orphanedRowAction with foreign keys as composite primary key are deleted when updated
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
- Define an entity
ParentwithOneToManyrelationship to another entityChild - Entity
Childhas a composite primary key: parent (Parent) id + another entity (OtherEntity) id - Save an object of type
Parentwith at least 1 child of typeChild. - Make changes in one of the
Childobjects 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.
because you set orphanedRowAction: delete so it was deleted by the relation
because you set
orphanedRowAction: deleteso 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.
because you set
orphanedRowAction: deleteso it was deleted by the relationI 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
because you set
orphanedRowAction: deleteso it was deleted by the relationI 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
childand value is[], then all old child will be remove by yourorphanedRowAction, 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.
does the children has primaryId in it ? otherwise typeorm will think these are new child and still remove your old children
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.
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
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).
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
I saw in the logs that the children were updated and then deleted in the end.
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