gpt4 book ai didi

sql - 如何创建包含多个字段的唯一索引,其中一个是外键

转载 作者:行者123 更新时间:2023-12-03 12:19:23 25 4
gpt4 key购买 nike

我正在尝试创建一个包含多个字段的索引,其中一个字段是另一个表的外键。但是我收到以下错误:

Error: Index "player_id_UNIQUE" contains column that is missing in the entity (Earning): 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 存在于实体中的索引进行识别。

最佳答案

这是完全没有记录的,所以在我偶然发现正确的语法之前花了一些时间。您实际上可以在索引定义中使用子属性:

@Index("player_id_UNIQUE", ["player.id", "period", "year"], { unique: true })
这样, player.id自动映射到 player_id在生成的 SQL 中:
CREATE UNIQUE INDEX "player_id_UNIQUE" ON "user_earning" ("player_id", "period", "year")

关于sql - 如何创建包含多个字段的唯一索引,其中一个是外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62373766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com