Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
Sequelize - foreign key constraints

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?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda