【发布时间】:2020-10-04 00:16:54
【问题描述】:
我正在尝试创建具有多个字段的索引,其中一个字段是另一个表的外键。但是我收到以下错误:
错误:索引“player_id_UNIQUE”包含在 实体(收入):player_id
鉴于 player_id 是我加入的外键,我该如何处理
import { Column, Entity, Index, JoinColumn, ManyToOne, PrimaryColumn } from "typeorm";
import { PersonPlayer } from "./PersonPlayer";
import { Team } from "./Team";
@Entity()
@Index("player_id_UNIQUE", ["player_id", "period", "year"], { unique: true })
export class Earning {
@PrimaryColumn({length: 36})
id: string;
@Column({nullable: true})
year: number;
@Column({type: 'decimal', nullable: true})
amount: number;
@Column({nullable: true, length: 45})
period: string;
@ManyToOne(() => Team, {nullable: true})
@JoinColumn({name: 'team_id'})
team: Team;
@ManyToOne(() => PersonPlayer, {nullable: true})
@JoinColumn({name: 'player_id'})
player: PersonPlayer;
@Column({nullable: true, length: 45})
dtype: string;
}
当我生成此实体并创建 sql 表(没有索引)时,我将 player_id 视为列之一。但是现在 typeorm 似乎无法通过 joincolumn 关系识别 player_id 存在于实体中的索引。
【问题讨论】:
-
您是否设法解决了这个问题@Kay?我现在遇到了同样的问题,在官方文档中找不到答案。
-
@VincentvanderWeele 是的,我将
player: personPlayer更改为player_id: PersonPlayer。这对我们有用,因为我们的项目只是处理数据库迁移,尽管将 player 作为名称而不是 player_id 会更好。但是因为我们没有使用 orm 的其余部分来制作我们不介意的任何业务逻辑