question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to delete subordinate relation entity when saving parent entity

See original GitHub issue

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [x] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x ] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

My intention was to remove the subordinate entities in a relation from the parent entity.

For example, I’ve a Photo entity which has a one-to-many relation setup with another entity MetaData. So, when I retreive a Photo entity with MetaData relation, the Photo entity should have a shape like this:

    const photoEntity = this.photoRepository.findOne({where: {id: 5}, relations: ['metaData'])
    photoEntity: {
       file: 'someimage.jpg',
       metaDatas: [
          {
             type: 'date',
             data: '12-12-2000'
          },
          {
             type: 'location',
             data: '123.00 123.00'
          }
       ]
    }

Basically, the metaDatas property in Photo entity will be an array. My intention is to remove all MetaData from the Photo entity.

What I did was simply by setting the array of photoEntity.metaDatas to an empty array and then save photoEntity like so:

    const photoEntity = this.photoRepository.findOne({where: {id: 5}, relations: ['metaData'])
    // One photo can have many metadata rows of information in photoEntity.metaDatas
    // Photo entity has a one-to-many relationship setup with MetaData entity
    photoEntity.metaDatas = []   //  Remove all metaDatas in photoEntity
    
    this.photoRepository.save(photoEntity)   // Throws error that ER_BAD_NULL_ERROR: Column 'photoId' cannot be null

However, this throws an error that Column 'photoId' cannot be null. This is because instead of running a DELETE query, the SQL TypeORM did an UPDATE this:

sql: 'UPDATE `metaData` SET `photoId` = NULL, `updatedAt` = CURRENT_TIMESTAMP WHERE `metaDataId` = `123` }

Shouldn’t a DELETE query be run instead of an update one? In this case, how can I remove the MetaData entities from the Photo entity?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:31
  • Comments:20 (4 by maintainers)

github_iconTop GitHub Comments

35reactions
douglasgsouzacommented, Mar 18, 2021

I used orphanedRowAction and it worked for me.

Parent:

@OneToMany(() => ConfiguracaoCotacaoDetalhe, detalhe => detalhe.configuracaoCotacao, {
        cascade: true
    })
    detalhes?: ConfiguracaoCotacaoDetalhe[];

Child:

    @ManyToOne(() => ConfiguracaoCotacao, config => config.detalhes, {orphanedRowAction: 'delete'})
    @JoinColumn({name: 'configuracaoCotacaoId'})
    configuracaoCotacao: ConfiguracaoCotacao;
4reactions
Kononnablecommented, Jun 12, 2019

I think you’re looking for this: https://github.com/typeorm/typeorm/issues/1460

Read more comments on GitHub >

github_iconTop Results From Across the Web

Child entity not deleting on removal of parent entity
When calling remove on the parent entity object, I recieve the error message: "The entity is still referenced elsewhere in the database". I...
Read more >
Defining and Using Relationships - InterSystems Documentation
Describes how to define and use relationships, a special kind of property used to define a one-to-many or parent-child relationship between persistent ...
Read more >
JPA: foreign key violation during parent entity deletion
I've a little JPA question, when I try to delete a parent entity an exception has always been thrown. ... REMOVE => cannot...
Read more >
2318852 - Employee Central - Position Management
2515752 - Error Deactivating a Position - You cannot deactivate the position because it has 1 lower-level positions between XXXX and XXXX that...
Read more >
Fix list for IBM WebSphere Application Server V8.5
PH34906, XML External Entity Injection (XXE) in WebSphere Application Server Java ... PH30851, Updating fix pack 8.5.5.15 with the interim fix PH25216 fails....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found