sequelize / sequelize-typescript

Decorators and some other features for sequelize

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

setting custom name for foreign key constraint

dev-mat opened this issue · comments

Issue

Cannot set custom foreign key constraint name

Versions

2.1.5

  • sequelize: 6.32.0
  • sequelize-typescript: 2.1.5
  • typescript: 4.7.4

Issue type

  • bug report
  • [ x] feature request

Actual behavior

Cannot set custom foreign key constraint name using decorators. Actually I am setting { constraints: false } in @BelongsTo decorator and adding foreign keys with specified names using queryInterface.

Expected behavior

I there an option to specify foreign key constraint name using decorators? If not, do you plan to add this feature ?

Steps to reproduce

Related code

  /* Table A */
  @HasMany(() => B)
  bs: B[]
  
  /* Table B */
  @ForeignKey(() => A)
  @Column({ allowNull: false })
  aId: number

  // I want to specify key name, for eg. 'FK_my_custom_named_key'
  @BelongsTo(() => A, { foreignKey: 'aId', constraints: false })
  a: A
  

According to inline documentation and the compiler itself the following syntax is acceptable, however it is my experience that this has no effect and this value is ignored.

    @BelongsTo(() => SomeOtherModel, {
        foreignKey: {
            field: 'someOtherModelId',
            name: 'ThisModel_SomeOtherModelId_foreign_idx'
        }
    })
    someOtherModel?: SomeOtherModel;

    @ForeignKey(() => SomeOtherModel)
    @Column({
        type: DataType.UUID,
        allowNull: false,
    })
    someOtherModelId!: string;

I am unsure if this is due to my local configuration due to its complexity so I have not yet raised this as a bug.