以错误的顺序重新排列表格
我在我的nodejs应用程序中使用sequelize ORM,它似乎是在我sequelize.sync({force: true})
以错误的顺序丢弃表
例如,用:
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})
我在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 a parent row: a foreign key constraint fails
似乎是以错误的顺序删除表。
与客人同样的答案,但没有外部要求。
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); });
在执行sequelize.sync({force: true})
之前禁用外键检查sequelize.sync({force: true})
例如:
async.series([ function(callback) { sequelize.query("SET FOREIGN_KEY_CHECKS = 0").complete(callback); }, function(callback) { sequelize.sync({force: true}).complete(callback); }, function(callback) { sequelize.query("SET FOREIGN_KEY_CHECKS = 1").complete(callback); }] , callback);
这对我工作:
sequelize.query('SET FOREIGN_KEY_CHECKS = 0').success(function() { sequelize .sync({ force: true }).success(function() { sequelize.query('SET FOREIGN_KEY_CHECKS = 1').success(function() { console.log('Database synchronised.'); }); }).error(function(err) { console.log(err); });; }).error(function(ee) { console.log(err); });
这适用于我的模型和关系。
/* the models in the array are declared on the basis of relationships */ var models = [ 'User', 'State', 'Party', 'Politician', 'Constituency', 'Award', 'Comment', 'Favorite', 'Rating' ]; models.forEach(function(model) { //need to drop things to make sure the order is maintained while droping tables... module.exports[model] = sequelize.import(__dirname + '/' + model); }); /*** setup relationships ***/ (function(m) { m.Award.belongsTo(m.Politician, {foreignKey: 'award_politician_id', as: 'politician_id',foreignKeyConstraint:true}); m.Politician.hasMany(m.Award, {foreignKey: 'award_politician_id',foreignKeyConstraint:true}); m.Comment.belongsTo(m.User, {foreignKey: 'comment_user_id', as: 'user_id',foreignKeyConstraint:true}); m.Comment.belongsTo(m.Politician, {foreignKey: 'comment_politician_id', foreignKeyConstraint:true}); m.Politician.hasMany(m.Comment, {foreignKey: 'comment_politician_id', foreignKeyConstraint:true}); m.User.hasMany(m.Comment, {foreignKey: 'comment_user_id', foreignKeyConstraint:true}); m.Favorite.belongsTo(m.User, {foreignKey: 'favorite_user_id', as: 'user_id',foreignKeyConstraint:true}); m.Favorite.belongsTo(m.Politician, {foreignKey: 'favorite_politician_id', as: 'politician_id',foreignKeyConstraint:true}); m.Politician.hasMany(m.Favorite,{foreignKey: 'favorite_politician_id', foreignKeyConstraint:true}); m.User.hasMany(m.Favorite, {foreignKey: 'favorite_user_id', foreignKeyConstraint:true}); m.Rating.belongsTo(m.User, {foreignKey: 'rating_user_id', as: 'user_id',foreignKeyConstraint:true}); m.Rating.belongsTo(m.Politician, {foreignKey: 'rating_politician_id', as: 'user_id',foreignKeyConstraint:true}); m.Politician.hasMany(m.Rating, {foreignKey: 'rating_politician_id', foreignKeyConstraint:true} ); m.User.hasMany(m.Rating, {foreignKey: 'rating_user_id', foreignKeyConstraint:true} ); m.Constituency.belongsTo(m.State, {foreignKey: 'constituency_state_id', as: 'state_id',foreignKeyConstraint:true}); m.State.hasMany(m.Constituency,{foreignKey: 'constituency_state_id', foreignKeyConstraint:true}); m.Politician.belongsTo(m.Party, { foreignKey: 'politician_party_id', as: 'party_id', foreignKeyConstraint:true}); m.Party.hasMany(m.Politician, {foreignKey: 'politician_party_id', foreignKeyConstraint:true}); // m.User.drop(); // // sequelize.sync(); })(module.exports); /** drop existing relationships manually in reverse order**/ models.reverse().forEach(function(model) { sequelize.import(__dirname + '/' + model).drop().success(function(){ console.log("success model:" + model); }).error(function(error){ console.log("model:" + model + " error:" + error); }); }); /** sync **/ sequelize.sync();
- 抛出exception杀死线程在节点JS?
- 我如何产生独立的孩子,并重新build立node.js通信?
- 运行npm install as sudo时,Git auth失败
- 为什么这个lambda函数需要括号?
- 我如何添加一个实例方法到sails.js中的所有模型?
- Mongoose API通过_id获取数组中的嵌套项
- 在开发stream程中Git npm版本pipe理
- 有一个networking主机,将允许我运行nodejs?
- nodejs https api请求错误错误:在TLSWrap.onread(net.js:568:26)处的exports._errnoException(util.js:1018:11)处读取ECONNRESET