Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Sequelize keeps creating duplicate unique constraints on the same column #17978

@Tawheed-Saifi

Description

@Tawheed-Saifi

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 defining a column with unique: true (either via the @unique decorator or directly in the @column options), Sequelize keeps creating duplicate unique constraints on the same column each time sequelize.sync({ alter: true }) runs.

Instead of detecting that the constraint already exists, Sequelize creates new constraints with incremented names like:

  • social_media_name_key
  • social_media_name_key1
  • social_media_name_key2
  • social_media_name_key3

This results in multiple redundant unique constraints being attached to the same column.

Reproducible Example

import {
  Table,
  Column,
  Model,
  DataType,
} from "sequelize-typescript";

@Table({ tableName: "social_media" })
export class SocialMedia extends Model {
  @Column({ type: DataType.STRING(30), unique: true, allowNull: false })
  name!: string;
}

After running sequelize.sync({ alter: true }) multiple times, the table accumulates duplicate constraints:

social_media_name_key
social_media_name_key1
social_media_name_key2
...

What do you expect to happen?

Sequelize should recognize that the unique constraint is already present and not add duplicates.

What is actually happening?

Sequelize generates a new unique constraint with an incremented name on each sync, leading to clutter in the schema.

Environment

  • Sequelize version: 6.37.7
  • Node.js version: 22.17.1
  • TypeScript version: 5.8.3
  • Database & Version: PostgreSQL Server 17
  • Connector library & Version: pg 8.16.0

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending-approvalBug reports that have not been verified yet, or feature requests that have not been accepted yet

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions