Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!