node.js - Sequelize drop table in wrong order -
i using sequelize orm in nodejs app , seems drops table in wrong order when sequelize.sync({force: true})
for example, with:
var stationentity = sequelize.define('station', { id: { type: sequelize.integer, primarykey: true, allownull: false}, name: { type: sequelize.string, allownull: false} }) var stationsnapshotentity = sequelize.define('stationsnapshot', { id: { type: sequelize.bigint, autoincrement: true, primarykey: true}, snapshottimestamp: { type: sequelize.bigint, allownull: false} }) stationentity.hasmany(stationsnapshotentity, {as: 'snapshots', foreignkeyconstraint: true, allownull: false})
i following logs after sequelize.sync({force: true})
:
executing: drop table if exists `stations`; executing: drop table if exists `stationsnapshots`; executing: create table if not exists `stationsnapshots` (`id` bigint auto_increment , `snapshottimestamp` bigint not null, `createdat` datetime not null, `updatedat` datetime not null, `stationid` integer, primary key (`id`), foreign key (`stationid`) references `stations` (`id`)) engine=innodb; error: er_row_is_referenced: cannot delete or update parent row: foreign key constraint fails
seems dropping tables in wrong order.
same answer guest without external requirements.
db.query('set foreign_key_checks = 0') .then(function(){ return db.sync({ force: true }); }) .then(function(){ return db.query('set foreign_key_checks = 1') }) .then(function(){ console.log('database synchronised.'); }, function(err){ console.log(err); });
Comments
Post a Comment