sql - Sequelize 自关联插入或更新表违反外键约束 fkey

标签 sql node.js sequelize.js

创建自关联时出现 SequelizeForeignKeyConstraintError

const Category=sequelize.define('Category',{
        name:{ type:DataTypes.STRING, defaultValue: '' },
        parentCategoryId:{ type:DataTypes.INTEGER},
        });

    Category.associate = (models) => {
        Category.hasMany(models.Category, {as: 'subCategories', foreignKey: 'parentCategoryId'});
        Category.belongsTo(models.Category, {as: 'parent', foreignKey: 'parentCategoryId'});
    };
Trace: { SequelizeForeignKeyConstraintError: insert or update on table  violates foreign key constraint fkey

最佳答案

删除关联列并再次同步

ALTER TABLE public."Categories" DROP "parentCategoryId"
然后再次与 sequilize 同步它会创建正确的关联
小心这会删除旧的 parentCategoryId cdata

关于sql - Sequelize 自关联插入或更新表违反外键约束 fkey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66572748/

相关文章:

sql-server - 使用 Sequelize 进行 NOLOCK 查询

java - SQLException : Operation not allowed after ResultSet closed

sql - 如何将计数和总和组表示为 SQL View 中的列?

javascript - nodeclipse Enide,删除关键字问题

node.js - 从控制台调试 Protractor 测试

node.js - 如何在 Sequelize 中创建表以使用 Node JS 存储在 Postgresql 中

javascript - 语法错误 : Unexpected token * after importing sequelize model db object

mysql - MariaDB,如何使用字段名作为表名?

mysql 不同查询不起作用

node.js - Sequelize 命中数据库,为每个事务命令提供新查询