MySQL Sequelize:销毁/删除表中的所有记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40952058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:05:56  来源:igfitidea点击:

Sequelize: Destroy/Delete all records in the table

mysqlnode.jssequelize.js

提问by user1107173

I am using Mocha for Unit tests.

我正在使用 Mocha 进行单元测试。

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?

如何删除/销毁表中的所有记录?

回答by maheshiv

You can try using

您可以尝试使用

db.User.destroy({
  where: {},
  truncate: true
})