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.

Elements from this relationship are not deleted when an empty array is set

See original GitHub issue

Issue type:

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

Database system/driver:

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

TypeORM version:

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

class ProductsModel extends ORM {
    @OneToMany(type => ProductProductPropertiesModel, prodProdProperies => prodProdProperies.product, {
        eager: true,
        cascade: true,
        onDelete: 'CASCADE',
    })
    @JoinTable({
        name: 'product-product-properties',
        joinColumn: {
            name: "product_id",
            referencedColumnName: "id"
        },
        inverseJoinColumn: {
            name: "propuct_property_id",
            referencedColumnName: "id"
        }
    })
    productProductProperties: ProductProductPropertiesModel[];
}

export class ProductProductPropertiesModel extends ORM {
    @ManyToOne(type => ProductsModel, product => product.id, { nullable: false })
    @JoinColumn({ name: 'product_id' })
    product: ProductsModel;}

-------------------------

product.productProductProperties = [];
product.save();

-------------------------
Unhandled Promise rejection: ER_BAD_NULL_ERROR: Column 'product_id' cannot be null ; Zone: <root> ; Task: Promise.then ; Value: { QueryFailedError: ER_BAD_NULL_ERROR: Column 'product_id' cannot be null

How to automatically delete these records?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:25 (7 by maintainers)

github_iconTop GitHub Comments

52reactions
i5ocommented, Mar 1, 2021

In case you reached here through Google, the solution that @nebkat provides does work. image

20reactions
kelvin-evaricommented, Mar 7, 2019

Sorry I dig up this one but I’m having the same problem.

So you have an error:

Column ‘product_id’ cannot be null

try to figure out why it says so

This is because whenever updating an array, typeorm compares the new and old array to find which records should be remove. However, it set the value of foreign key to be null instead of removing those records: UPDATE "ProductProductPropertiesModel" SET "product_id" = $2 WHERE "id" = $1 -- PARAMETERS: [<id of should-be-deleted record>,null]. Since this column is set to nullable: false, this query causes the issue.

I have to remove those old records manually, which I don’t think is a good approach. Not sure why typeorm doesn’t remove them instead?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove empty elements from an array in Javascript
A few simple ways: var arr = [1,2,,3,,-3,null,,0,,undefined,4,,4,,5,,6,,,,]; arr.filter(n => n) // [1, 2, 3, -3, 4, 4, 5, 6] arr.filter(Number) // [1,...
Read more >
Array - JavaScript - MDN Web Docs
JavaScript arrays are not associative arrays and so, array elements cannot be accessed using arbitrary strings as indexes, but must be accessed ...
Read more >
Array | Apple Developer Documentation
To remove elements from an array, use the remove(at:) , removeSubrange(_:) , and removeLast() methods. You can replace an existing element with a...
Read more >
Python Sets Tutorial: Set Operations & Sets vs Lists - DataCamp
Because sets cannot have multiple occurrences of the same element, it makes sets highly useful to efficiently remove duplicate values from a list...
Read more >
Why you should not remove all the elements of array by ...
Understand the tricky behavior while making array empty and learn how to fix it. ... Many times there comes a time when we...
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