Sequelize: Destroy/Delete all records in the table
Mysqlnode.jssequelize.jsMysql Problem Overview
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?
Mysql Solutions
Solution 1 - Mysql
You can try using
db.User.destroy({
where: {},
truncate: true
})
Solution 2 - Mysql
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.
Solution 3 - Mysql
It works for me: db.User.truncate()