Search code examples
postgresqltypeorm

TypeORM create constraint based on enum column and FK column?


I have the following table:

enter image description here

I must set a rule in that entity, so you can't have the same home_account_user_id and same account_status twice for example:

id | home_account_user_id | account_status
0  | 1                    | AWAY
0  | 1                    | AWAY

Should be:

id | home_account_user_id | account_status
0  | 1                    | AWAY
0  | 1                    | CLOSED

Is there a way to define it this way?


Solution

  • You could use UNIQUE constraint/index:

    CREATE UNIQUE INDEX udx_name ON tab_name(home_account_user_id, account_status);