sequelize icon indicating copy to clipboard operation
sequelize copied to clipboard

[BUG] Sequelize v6 Migration unique Constraint Object Ignored

Open djedu28 opened this issue 5 months ago • 0 comments

Issue Creation Checklist

  • [x] I understand that my issue will be automatically closed if I don't fill in the requested information
  • [x] I have read the contribution guidelines

Bug Description

When creating a migration with the unique constraint specified as the object, the unique constraint is ignored when creating tables using Sequelize v6. This results in the unique restriction not being applied correctly in the database schema. (I used it in SQLite)

Reproducible Example

_test_unique

module.exports = {
  up: async (queryInterface, { DataTypes }) => {
    return queryInterface.createTable('users', {
      username: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: {
          value: true,
          msg: "Username already registered"
        },
      },
      // other fields...
    });
  },
};

return CREATE TABLE IF NOT EXISTS `users` (`username` VARCHAR(255) NOT NULL);

What do you expect to happen?

The unique constraint should be applied to the username field, preventing duplicate entries in the database.

CREATE TABLE IF NOT EXISTS `users` (`username` VARCHAR(255) NOT NULL UNIQUE);

What is actually happening?

When running this migration, the unique constraint is not enforced. The database table is created without the expected unique index on the username column.

To reproduce:

  1. Run the migration script
  2. Attempt to insert two identical usernames into the users table
  3. Observe that the second insertion succeeds despite the unique constraint

The error log or console output will show that the migration was successful, but the unique constraint is not present in the resulting database schema.

This issue affects all migrations where the unique constraint is specified as an object instead of a boolean value. It impacts the integrity of the database schema and may lead to data inconsistencies if not addressed.

In the documentation, unique can be an object containing the msg and name. I understand that the parameters of this object are used exclusively by the Model (at the JavaScript level, not the database). However, it is expected that the settings in the Model will be copied and inserted into the migration, and therefore be compatible with each other. Furthermore, the provided typing import('sequelize-cli').Migration also provides the option to send the object — see the image below.

Image

Environment

  • Sequelize version: "6.37.7" and sequelize-cli: "6.6.3"
  • Node.js version: v22.16.0
  • ~If TypeScript related: TypeScript version:~
  • Database & Version: sqlite3@npm:5.1.7
  • Connector library & Version:

Would you be willing to resolve this issue by submitting a Pull Request?

  • [ ] Yes, I have the time and I know how to start.
  • [x] Yes, I have the time but I will need guidance.
  • [ ] No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • [ ] No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

djedu28 avatar Jul 20 '25 07:07 djedu28