admin管理员组文章数量:1206189
In Sequelize, I am using this function model.destory({ truncate: true }), it delete all data in table. But the issue is that it does not reset the primary key sequence in table which should be set to Zero. I am using Mysql. Some said that Mysql automatically reset the primary key sequence, but it is not happening in my case.
Here is my code:
db.Booking.destroy({ truncate: { cascade: false } })
.then(() => {
res.json({ status: true });
}, (err) => {
console.log('truncate: ', err);
res.json(err);
});
In Sequelize, I am using this function model.destory({ truncate: true }), it delete all data in table. But the issue is that it does not reset the primary key sequence in table which should be set to Zero. I am using Mysql. Some said that Mysql automatically reset the primary key sequence, but it is not happening in my case.
Here is my code:
db.Booking.destroy({ truncate: { cascade: false } })
.then(() => {
res.json({ status: true });
}, (err) => {
console.log('truncate: ', err);
res.json(err);
});
Share
Improve this question
edited Nov 2, 2020 at 13:59
coder
9061 gold badge12 silver badges19 bronze badges
asked Sep 29, 2016 at 8:46
user6898264user6898264
1
- Related for SQLite: stackoverflow.com/questions/55966627/… – Ciro Santilli OurBigBook.com Commented Jun 17, 2021 at 10:38
2 Answers
Reset to default 19You're not using the correct syntax:
db.Booking.destroy({ truncate: { cascade: false } })
That should be:
db.Booking.destroy({ truncate : true, cascade: false })
See the documentation.
if you are, in any case, using a custom foreign key, use this instead
db.Booking.truncate({cascade: true, restartIdentity:true})
as destroy
would not work at will with custom foreign key
this one is nested pretty deep in the documentation
See here
本文标签: javascriptIn Sequelize modeldestroy( truncate true ) does not reset primary keyStack Overflow
版权声明:本文标题:javascript - In Sequelize model.destroy({ truncate: true }) does not reset primary key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738719311a2108647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论