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

244
Vistas
Sequelize MySQL Migration - unique to false

I am trying to write a migration in Sequelize and MySQL, which sets the unique attribute to false. This is my approach so far:

module.exports = {
  up: async (queryInterface, Sequelize) => {
          await queryInterface.changeColumn("users", "name", {
              type: Sequelize.STRING,
              allowNull: false,
              unique: true,
          });
          await queryInterface.changeColumn("users", "email", {
              type: Sequelize.STRING,
              allowNull: false,
              unique: true,
         });
    },

 down: async (queryInterface, Sequelize) => {
          await queryInterface.changeColumn("users", "name", {
            type: Sequelize.STRING,
            allowNull: true,
            unique: false,
      });
          await queryInterface.changeColumn("users", "email", {
            type: Sequelize.STRING,
            allowNull: true,
            unique: false,
   });
  },
};

The up migration works like charme, the down migration works for allowNull, but not for unique attribute. I am new to Sequelize so I am wondering, what is going wrong here. Can someone help me out?

Thank you very much in advance.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Sequelize has "removeConstraint" method, using which you will be able to remove the constraint for the attribute.

public removeConstraint(tableName: string, constraintName: string, options: Object): Promise

Actually, if you don't specify the "constraintName" in creating, it should be of the form "attributename_unique_key".

Please try using this way

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.changeColumn("users", "name", {
      type: Sequelize.STRING,
      allowNull: false,
      unique: true,
    });
    await queryInterface.changeColumn("users", "email", {
      type: Sequelize.STRING,
      allowNull: false,
      unique: true,
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.changeColumn("users", "name", {
      type: Sequelize.STRING,
      allowNull: true,
    });
    await queryInterface.removeConstraint("users", "name_unique_key");
    await queryInterface.changeColumn("users", "email", {
      type: Sequelize.STRING,
      allowNull: true,
    });
    await queryInterface.removeConstraint("users", "email_unique_key");
  },
};

For more information please check the documentation here

over 4 years ago · Santiago Trujillo Denunciar
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