In Sequelize model.destroy({ truncate: true }) does not reset primary key

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); }); 
1

2 Answers

You'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.

3

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like