I am working with the following database entities:
@Entity()
class Team {
@PrimaryGeneratedColumn()
id: string
@PrimaryColumn()
teamName: string
@OneToMany(() => Player, player => player.team)
players: Player[]
}
@Entity()
class Player {
@PrimaryGeneratedColumn()
id: string
@ManyToOne(() => Team)
team: Team
}
While using Typeorm, I encountered an error regarding foreign key implementation. It seems that the issue stems from the fact that the Player entity is trying to reference a composite primary key in Team instead of just the id. One solution would be to change the primary key structure in Team, but I am curious if there are other approaches to tackle this problem. I attempted the following modification without success:
@Entity()
class Player {
@PrimaryGeneratedColumn()
id: string
@ManyToOne(() => Team)
@JoinColumn([
{name: 'teamId', referencedColumnName: 'id'},
{name: 'teamName', referencedColumnName: 'teamName'}
])
team: Team
}