Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define Table name using `sequelize-typescript`?

it used to be possible to define tableName parameter in the Table decorator from sequelize-typescript like the below:

@Table({
  tableName: 'my-custom-tablename'
})
export class Tenants extends Model<Tenants> {
  @IsUUID(4)
  @Default(uuid())
  @PrimaryKey
  @Column
  uuid!: string;

  @CreatedAt
  @Column
  created_at!: Date;

  @UpdatedAt
  @Column
  updated_at!: Date;
}

With the latest version it doesn't seem possible to do this, only two options remain available: modelName and version, so now, TableName is automatically mapped to the ModelName (className)

How to pass the real table name associated to the model?

like image 870
dbrrt Avatar asked Feb 04 '26 07:02

dbrrt


1 Answers

@Table({
  tableName: 'user'
})
export class User extends Model<User> {
  @Column
  firstName!: string;

  @CreatedAt
  @Column
  createdAt!: Date;

  @UpdatedAt
  @Column
  updatedAt!: Date;

}

This is working for me on the latest npm versions for sequelize-typescript (1.1.0) and sequalize (5.21.11).

On default the table would be named Users but with this code it is set to user.

like image 54
Mkk Avatar answered Feb 07 '26 00:02

Mkk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!