sequelize / sequelize/sequelize

[BUG] Sequelize v6 Migration unique Constraint Object Ignored

Open
#17,940 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

pending-approval
Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • 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.
  • 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the migration's queryInterface.createTable entry point and reproduce the supplied JavaScript example against sqlite3@5.1.7, comparing the generated CREATE TABLE statement with the expected UNIQUE clause. Done means object-form unique settings produce an enforced uniqueness constraint; verify this by attempting duplicate username inserts and checking the resulting schema.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, sqlite, typescript
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.