如何在TypeORM中为不同列创建独立的唯一约束?
解决方案:为TypeORM实体字段创建独立唯一约束
当然可以,你可以通过两种方式为userGUID和userName分别创建独立的唯一约束,完全匹配你提供的SQL定义:
方法1:多次使用@Unique装饰器
在实体类上多次添加@Unique装饰器,每个装饰器对应一个独立的唯一约束,并指定自定义的约束名称(与你SQL中的约束名一致):
import { Column, Entity, PrimaryColumn, Unique } from "typeorm"; import { UserProfileORM } from "./userprofile"; @Entity({ name: "USERS" }) @Unique("users_uq_username", ["userName"]) // 单独为userName创建唯一约束 @Unique("users_uq_userguid", ["userGUID"]) // 单独为userGUID创建唯一约束 export class UserORM { @PrimaryColumn({ name: "ID", type: "number", primaryKeyConstraintName: "USERS_PK", }) id!: number; @Column({ name: "USERGUID", type: "varchar2", length: 256, nullable: false, }) userGUID!: string; @Column({ name: "USERNAME", nullable: false, type: "varchar2", length: 256 }) userName!: string; // 以下为其他字段,保持原有定义不变 @Column({ name: "EMPLOYEEID", nullable: false, type: "varchar2", length: 256 }) employeeID!: string; @Column({ name: "EMAILID", nullable: false, type: "varchar2", length: 256 }) emailID!: string; @Column({ name: "FIRSTNAME", nullable: false, type: "varchar2", length: 256, }) firstName!: string; @Column({ name: "MIDDLENAME", nullable: true, type: "varchar2", length: 256, }) middleName?: string; @Column({ name: "LASTNAME", nullable: false, type: "varchar2", length: 256 }) lastName!: string; @Column({ name: "MANAGERDN", nullable: true, type: "varchar2", length: 4000, }) managerDN?: string; @Column({ default: "INACTIVE", name: "STATUS", nullable: false, type: "varchar2", length: 256, }) status!: string; @Column({ name: "CREATEDBY", nullable: false, type: "varchar2", length: 256, }) createdBy!: string; @Column({ default: () => `CAST(systimestamp AT TIME ZONE 'UTC' AS TIMESTAMP WITH TIME ZONE)`, name: "CREATIONDATE", nullable: false, type: "timestamp with time zone", }) creationDate!: Date; @Column({ default: "0.0.0.0", name: "SOURCEIP", nullable: false, type: "varchar2", length: 256, }) sourceIP?: string; @Column({ name: "LASTUPDATEDBY", nullable: true, type: "varchar2", length: 256, }) lastUpdatedBy?: string; @Column({ name: "LASTUPDATEDDATE", nullable: true, type: "timestamp with time zone", }) lastUpdatedDate?: Date; }
每个@Unique装饰器单独声明一个约束,第一个参数是约束的自定义名称,第二个参数是单个字段的数组,这样就能生成两个独立的唯一约束,而非联合约束。
方法2:在@Column装饰器中直接配置
在字段的@Column装饰器中设置unique: true,并通过uniqueConstraintName指定约束名称(该参数支持TypeORM 0.3及以上版本):
import { Column, Entity, PrimaryColumn } from "typeorm"; import { UserProfileORM } from "./userprofile"; @Entity({ name: "USERS" }) export class UserORM { @PrimaryColumn({ name: "ID", type: "number", primaryKeyConstraintName: "USERS_PK", }) id!: number; @Column({ name: "USERGUID", type: "varchar2", length: 256, nullable: false, unique: true, uniqueConstraintName: "users_uq_userguid" // 指定约束名称 }) userGUID!: string; @Column({ name: "USERNAME", nullable: false, type: "varchar2", length: 256, unique: true, uniqueConstraintName: "users_uq_username" // 指定约束名称 }) userName!: string; // 以下为其他字段,保持原有定义不变 @Column({ name: "EMPLOYEEID", nullable: false, type: "varchar2", length: 256 }) employeeID!: string; @Column({ name: "EMAILID", nullable: false, type: "varchar2", length: 256 }) emailID!: string; @Column({ name: "FIRSTNAME", nullable: false, type: "varchar2", length: 256, }) firstName!: string; @Column({ name: "MIDDLENAME", nullable: true, type: "varchar2", length: 256, }) middleName?: string; @Column({ name: "LASTNAME", nullable: false, type: "varchar2", length: 256 }) lastName!: string; @Column({ name: "MANAGERDN", nullable: true, type: "varchar2", length: 4000, }) managerDN?: string; @Column({ default: "INACTIVE", name: "STATUS", nullable: false, type: "varchar2", length: 256, }) status!: string; @Column({ name: "CREATEDBY", nullable: false, type: "varchar2", length: 256, }) createdBy!: string; @Column({ default: () => `CAST(systimestamp AT TIME ZONE 'UTC' AS TIMESTAMP WITH TIME ZONE)`, name: "CREATIONDATE", nullable: false, type: "timestamp with time zone", }) creationDate!: Date; @Column({ default: "0.0.0.0", name: "SOURCEIP", nullable: false, type: "varchar2", length: 256, }) sourceIP?: string; @Column({ name: "LASTUPDATEDBY", nullable: true, type: "varchar2", length: 256, }) lastUpdatedBy?: string; @Column({ name: "LASTUPDATEDDATE", nullable: true, type: "timestamp with time zone", }) lastUpdatedDate?: Date; }
这种方式直接在字段级别配置唯一约束,更直观,同时通过uniqueConstraintName确保生成的约束名称和你提供的SQL完全一致。
内容的提问来源于stack exchange,提问作者Ranjeet
相关产品推荐
相关产品推荐

