typeorm - 如何在 TypeORM 中使用可为空字段创建唯一约束?
问题描述
如何在 TypeORM 中使用可为空字段创建唯一约束?
我使用了 postgresql12,节点 v12.22.7。
创建单独的原始查询会更好吗?
没有迁移工具?
如果我使用原始查询,我可以像下面的地址一样使用它吗?
这是我的代码示例。
@Entity('users')
@Unique('username-unique',['username', 'deletedAt'])
export class User {
@PrimaryGeneratedColumn()
id: number
@Column({ length: 100 })
username: string
@Column({ length: 100 })
name: string
// sha256
@Exclude()
@Column({ type: 'bytea', select: false})
password: Buffer
@Column({
nullable: true,
type: 'timestamp with time zone'
})
lastLoginAt: Date | null
@OneToMany(() => RoleBindingEntity, (roleBinding) => roleBinding.user, { cascade: true })
@JoinColumn()
roleBinding: RoleBindingEntity[]
@CreateDateColumn({
type: 'timestamp with time zone',
default: () => 'CURRENT_TIMESTAMP'
})
createdAt: Date
@UpdateDateColumn({
type: 'timestamp with time zone',
default: () => 'CURRENT_TIMESTAMP'
})
updatedAt: Date
@DeleteDateColumn({
type: 'timestamp with time zone'
nullable: true,
})
deletedAt: Date
}
解决方案
推荐阅读
- elasticsearch - 嵌套关键字字段上的 Elasticsearch 范围查询
- react-native - react-native 中是否有任何新的良好导航资源?
- visual-studio-2017 - TFS 2018 构建定义在团队资源管理器面板下的 vs2017 中不可见
- r - 使用 google_places 进行多个位置搜索(来自 googleway)
- vba - 为什么我的 VBA 代码中出现数据不匹配错误
- gremlin - How to add a default value for empty traversals in gremlin?
- reporting-services - 将 SSRS 中的排序顺序箭头从升序更改为降序
- java - 运行两个程序并通过 Java 服务器处理 I/O
- php - 更新数据库成功时如何设置变量“true”?
- spring-boot - 如何将 Web 应用程序文件夹加载到 docker 容器