I am using Mocha for Unit tests.
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(() => {
}).then(() => done());
db.User.destroy(
{where: undefined},
{truncate: false}
).then(() => {
return
}).then(() => done());
db.User.destroy({}).then(() => {
return db.User.bulkCreate(users)
}).then(() => done());
I keep getting the following error:
Error: Missing where or truncate attribute in the options parameter of model.destroy.
How do I delete/destroy all the records in a table?
To delete rows of data from your SQL table using Sequelize, you need to use the provided destroy() method. The destroy() method can be called from any Model or instance of your Model to delete rows from your table.
Some Sequelize commands don't return anything (or at least not anything useful) by default. Sequelize uses an option called returning to specify which data returns from a commands like . destroy() or update() . Let's look at three common Sequelize commands and see how to handle the data returned by each.
destory({ truncate: true }), it delete all data in table.
Sequelize supports the concept of paranoid tables. A paranoid table is one that, when told to delete a record, it will not truly delete it. Instead, a special column called deletedAt will have its value set to the timestamp of that deletion request.
You can try using
db.User.destroy({
where: {},
truncate: true
})
I was able to solve this problem with the code:
table.sync({ force: true });
This is a safer solution than the one proposed in maheshiv's answer.
It works for me: db.User.truncate()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With