Elements from this relationship are not deleted when an empty array is set
See original GitHub issueIssue 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:
- Created 5 years ago
- Reactions:4
- Comments:25 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In case you reached here through Google, the solution that @nebkat provides does work.
Sorry I dig up this one but I’m having the same problem.
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 tonullable: 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?