typeOrm 唯一行

Ric*_*des 1 typeorm nestjs

我正在尝试在我的 NestJS 上使用 typeOrm 创建一个实体,但它没有按我预期的那样工作。

我有以下实体

@Entity('TableOne')
export class TableOneModel {
  @PrimaryGeneratedColumn()
  id: number

  @PrimaryColumn()
  tableTwoID: number

  @PrimaryColumn()
  tableThreeID: number

  @CreateDateColumn()
  createdAt?: Date
}
Run Code Online (Sandbox Code Playgroud)

此代码生成一个迁移,该迁移生成一个类似于下面示例的表

+--------------+-------------+------+-----+----------------------+-------+
| Field        | Type        | Null | Key | Default              | Extra |
+--------------+-------------+------+-----+----------------------+-------+
| id           | int(11)     | NO   |     | NULL                 |       |
| tableTwoID   | int(11)     | NO   |     | NULL                 |       |
| tableThreeID | int(11)     | NO   |     | NULL                 |       |
| createdAt    | datetime(6) | NO   |     | CURRENT_TIMESTAMP(6) |       |
+--------------+-------------+------+-----+----------------------+-------+
Run Code Online (Sandbox Code Playgroud)

没关系,问题是,我想让表只允许一行tableTwoIDtableThreeID,我应该在实体中使用什么来生成我预期的表?

预计不允许像下面的例子这样的行

+----+------------+--------------+----------------------------+
| id | tableTwoID | tableThreeID | createdAt                  |
+----+------------+--------------+----------------------------+
|  1 |          1 |            1 | 2019-10-30 19:27:43.054844 |
|  2 |          1 |            1 | 2019-10-30 19:27:43.819174 |    <- should not allow the insert of this row
+----+------------+--------------+----------------------------+
Run Code Online (Sandbox Code Playgroud)

小智 5

尝试将该列标记为唯一

@Unique() 列名