创建自关联时出现 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/