sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

Setting unique on @Column creates runaway index creation

Open dever23b opened this issue 3 years ago • 0 comments

Issue

There seems to be a bug associated with defining index properties in the @Column decorator.

Moving the unique option into an @Index decorator resolves the issue.

Versions

  • sequelize: 6.21.4
  • sequelize-typescript: 2.1.3
  • typescript: 4.8.2

Issue type

  • [x] bug report
  • [ ] feature request

Actual behavior

When I include unique: true in @Column, it creates a runaway condition whereby Sequelize creates a new unique index every sync until the DB eventually rejects it. In my case, I'm developing a project that uses MariaDB and, while still tweaking things, I have some of my models configured to sync on every run. I became aware of the issue because I started receiving 'too many keys' errors during startup. Upon further investigation, I found several tables had hit the max allowable indexes, with 60+ redundant indexes defined for the same unique column.

Expected behavior

The column is configured in such a way that, when Sequelize reviews it later, it is able to identify any already-existing index and not continue to re-create redundant ones. The behavior shouldn't be any different between defining the index using @Column vs. @Index.

Steps to reproduce

This code will produce the runaway situation

@Column({
  type: DataType.INTEGER,
  unique: true
})
declare foo_id: number;

Related code

This code works

@Index({ unique: true })
@Column({
  type: DataType.INTEGER
})
declare foo_id: number;

dever23b avatar Sep 08 '22 19:09 dever23b