Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

442
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda