简体   繁体   中英

How do I make combination of 3 columns in typeorm, postgres unique?

How can I achieve that there aren't 2 same records in db with values of these 3 columns combined being the same?

  @Column()
  sector: string;

  @Column()
  row: string;

  @Column()
  number: string;

Updated Answer:

While creating a unique @Index like @Johannes suggested works for you, semantically the better solution would be creating a composite unique key .

@Unique(["sector", "row", "number"])

Based on https://stackoverflow.com/a/23665806/4343332 , Postgres seems to handles both Unique Index and Unique Constraint in the same way internally.

Hence there will be no performance benefit in choosing either one.

Thank you for the insight @Johannes H.

You can annotate the entity with an @Index as described in the documentation about "Indices with multiple columns" :

@Index(["sector", "row", "number"], { unique: true })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM