Search code examples
typeormnode.js-typeorm

How to specify constraint name in TypeOrm for postgresql


I would like to follow SQL naming standards for Primary and Foreign Key names. One such approach is in Naming conventions in SQL. For the Primary key, the name should be in the format PK_. The PrimaryGeneratedColumn decorator should allow you to enter the name. For Foreign Key, the name should be in the format FK__. This could be in the relation decorator.

I cannot see how to achieve this.

Currently the constraints names do not have any semantic meaning - FK_f38670e509f911de73d91cab5bb

Could you let me know if there is another way to achieve this?

Many thanks


Solution

  • You can use @Unique() constraint as follows:

    @Entity()
    @Unique('UNIQUE_EMAIL_ADDRESS', ['address'])
    export class Email {
      @PrimaryGeneratedColumn('uuid')
      id: string;
    
      @Column({ type: 'varchar', length: 64 })
      address: string;
    }