I have a problem while undoing my migrations using sequelize-cli.
I get this error :
== 20211023095522-create-product: reverting =======
ERROR: Cannot delete or update a parent row: a foreign key constraint fails
Here is my model:
"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class Product extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
this.hasOne(models.ProductInfo, {
foreignKey: "product_id",
});
this.belongsToMany(models.Size, {
through: "product_size",
foreignKey: "product_id",
});
this.belongsToMany(models.Category, {
through: "product_category",
foreignKey: "product_id",
});
this.belongsToMany(models.Color, {
through: "product_color",
foreignKey: "product_id",
});
}
toJSON() {
return {
...this.get(),
updatedAt: undefined,
};
}
}
Product.init(
{
stock: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
},
{
sequelize,
modelName: "Product",
}
);
return Product;
};
And here is an associated model of product (they are all associated the same way):
"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class Size extends Model {
static associate(models) {
// define association here
this.belongsToMany(models.Product, {
through: "product_size",
foreignKey: "size_id",
});
}
}
Size.init(
{
name: { type: DataTypes.STRING, allowNull: false },
},
{
sequelize,
modelName: "Size",
}
);
return Size;
};
I tried adding onDelete: "Cascade" and onUpdate: "CASCADE" in each of my associations parameters but it didn't solved my problem.
Do you have any idea?