Nodejs sequelize how to truncate a foreign key referenced table
This is happening, because sequelize will not allow you to execute multiple queries with a single sequelize.query
call. The described scenario is handled right now only for sequelize.sync({ force: true })
which drops all tables and recreates them afterwards. The method is internally using the following code:
https://github.com/sequelize/sequelize/blob/a014bd8d172fb8fd9881cee866abfcab842c30fc/lib/query-interface.js#L227-228
It basically loads every table and checks if there are foreign keys. If that is the case, sequelize will drop them before the other table. You could probably adopt the logic. Furthermore: If you decide to implement that stuff, you could probably open a pull request on github. That would rock hard. Another option which would probably work, is the following:
sequelize.transaction(function(t) {
var options = { raw: true, transaction: t }
sequelize
.query('SET FOREIGN_KEY_CHECKS = 0', null, options)
.then(function() {
return sequelize.query('truncate table myTable', null, options)
})
.then(function() {
return sequelize.query('SET FOREIGN_KEY_CHECKS = 1', null, options)
})
.then(function() {
return t.commit()
})
}).success(function() {
// go on here ...
})
This works because transactions are using a dedicated connection, meaning you can easily execute commands in a row.
I got this by looking at another question and it worked for me on v4.13.2
MyTableModel.destroy({ truncate: { cascade: true } });