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

Define unique index on multiple columns in sequelize

Open bennettl opened this issue 4 years ago • 2 comments

Is this feature supported?

bennettl avatar Apr 13 '21 21:04 bennettl

yes it is, just give the index the same name on the fields. https://www.npmjs.com/package/sequelize-typescript#index

eg.


@Table
export class UserObject extends Model{
  
  @Index({
    name:'UserIdObjectId',
    unique:true,
  })
  @Column
  public userId!: number;

  @Index({
    name:'UserIdObjectId',
    unique:true,
  })
  @Column
  public objectId!: number;
}

or specify the index on the table attribute

@Table({
  indexes:[
    { fields: ['userId','objectId'], unique:true}
  ]
})
export class UserObject extends Model{
  @Column
  public userId!: number;
  @Column
  public objectId!: number;
}

michan85 avatar May 18 '21 08:05 michan85

could it be that the second method ({ fields: ['userId','objectId'], unique:true}) not only adds a composite index, but also 2 single indexes on the individual columns?

is this behavior intended or just not well documented ?

dberardo-com avatar Jul 20 '23 06:07 dberardo-com