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.

Support for destroy() to DELETE ALL entities of the given model

See original GitHub issue

Currently (v. 0.9.2), the documentation on model.destroy() says:

destroy performs a delete on the model, using the model’s idAttribute to constrain the query.

Without a constraining idAttribute, destroy() fails with the error message

A model cannot be destroyed without a "where" clause or an idAttribute.

However, it would be desirable to be able to actually do a

DELETE FROM MY_MODEL

…deleting all entities of the given model.

Yes, it’s a dangerous operation, but a useful one for cleanup / prototyping purposes. If you really want to keep an additional security net in the API, please consider adding a flag to destroy() to override the “idAttribute constraint” such that we can purposefully do a “delete all”.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
codebulbcommented, Feb 28, 2016

OK, so I found a workaround. The obvious solution is to use a where() clause that is always true:

myModel.where('id', '!=', '0').destroy().then(...)

This settles my interest in this issue. Feel free to close it.

0reactions
gambhirpulkitcommented, May 3, 2019

Alternatively, please suggest how to implement this with the current API.

I tried it with

const destroyed = []
myModel.fetchAll().then(function (collection) {
  collection.each(function(it) {
    destroyed.push(it.destroy())
  })
})
Promise.all(destroyed).then(function (entity) {
  // ok
}).catch(function (error) {
  // failed
})

but the problem is that if one of the it.destroy() fails (e.g. because of a foreign key violation), the entire thing fails with

Unhandled rejection Error: ER_ROW_IS_REFERENCED_2: Cannot delete or update a parent row: a foreign key constraint fails...

(Note: Maybe I didn’t exactly understand how to work with promises in this case.)

Use this - https://github.com/seegno/bookshelf-cascade-delete

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize: Destroy/Delete all records in the table
When testing begins, I would like to delete all the previous records in a table. What I have tried: db.User.destroy({ force: true }).then(()...
Read more >
[SOLVED] What is the best way to destroy an Entity from the ...
I'm trying to delete some cubes on a game after pressing a button. ... That is strange, entity.destroy() would definitely remove all entity...
Read more >
Deleting items in Rails
In this article, we are going to see the different methods to delete items in Rails. It's quite easy to confuse between destroy,...
Read more >
How to Delete Multiple Records Using Laravel Eloquent
I want to be able to delete multiple records from the database. I have the id 's of all the records I wish...
Read more >
Deleting Data - 4.x - CakePHP Cookbook
deleteAll will not trigger beforeDelete/afterDelete events. If you need callbacks triggered, first load the entities with find() and delete them in a loop....
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