【问题标题】:Unhandled rejection SequelizeForeignKeyConstraintError: insert or update on table未处理的拒绝 SequelizeForeignKeyConstraintError:在表上插入或更新
【发布时间】:2018-11-19 09:50:26
【问题描述】:

我使用 sequelize-cli 进行迁移:

我的迁移代码:

module.exports = {
 up(queryInterface, Sequelize) {
   queryInterface.showAllSchemas({
     options: {},
   }).then((data) => {
     data.forEach(schema => queryInterface.addColumn({
       tableName: 'visitors',
       schema, // dynamic schemas coming from loop
     }, 'purposeId', {
       type: Sequelize.INTEGER,
       references: {
         model: 'purposes', // name of Target model
         key: 'id', // key in Target model that we're referencing
       },
     }));
   });
 },
};

我有多个模式,其中我有一个名为“visitors”的表,在该表中我想从“目的”表中添加名为“目的 ID”的列。

迁移成功完成,我的“visitors”表中有一个 purposeId 列。

但是,如果我在“访问者”表中执行粗略操作,则会收到此错误:

未处理的拒绝 SequelizeForeignKeyConstraintError:插入或更新表“visitors”违反了外键约束“visitors_ purposeId_fkey”

注意:我正在使用 postgres。

【问题讨论】:

    标签: postgresql sequelize.js sequelize-cli


    【解决方案1】:

    实际上我使用约束解决了这个问题:false。

    我从迁移中删除了这些引用。

    我的迁移代码:

    module.exports = {
     up(queryInterface, Sequelize) {
       queryInterface.showAllSchemas({
         options: {},
       }).then((data) => {
         data.forEach(schema => queryInterface.addColumn({
           tableName: 'visitors',
           schema,
         }, 'purposeId', {
           type: Sequelize.INTEGER,
         }));
       });
     },
    
     down(queryInterface, Sequelize) {
       queryInterface.showAllSchemas({
         options: {},
       }).then((data) => {
         data.forEach(schema => queryInterface.removeColumn({
           tableName: 'visitors',
           schema,
         }, 'purposeId', {
           type: Sequelize.INTEGER,
         }));
       });
     },
    };
    

    在我的 Sequelize 模型中:

    Visitor.belongsTo(Purpose, { constraints: false });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 2021-10-20
      • 2019-03-15
      • 2016-07-21
      • 2021-12-30
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多