所以我有无法解决的问题,也许您可以帮助我。 所以:我有三个实体-User,Team,UserTeam,其中包含(userId,teamId,teamRole)。 UserTeam是包含复合主键的实体。 就像下面的代码一样:
@Index(["teamId", "userId"], { unique: true })
export class UserTeam {
@PrimaryColumn("int")
userId = undefined;
@PrimaryColumn("int")
teamId = undefined;
@ManyToOne(type => Team)
team = undefined;
@ManyToOne(type => User)
user = undefined;
@Column({
type: 'enum',
enum: ['CAPITAN', 'PLAYER'],
})
teamRole = "";
}
export class Team {
@PrimaryGeneratedColumn()
id = undefined;
@OneToMany(type => UserTeam, userTeam => userTeam.user)
userTeams = undefined;
export class User {
@PrimaryGeneratedColumn()
id = undefined;
@OneToMany(type => UserTeam, userTeam => userTeam.team)
teams = undefined;
}
所以->我想创建一个查询,该查询将退回特定团队的所有球员(用户)。因此我创建了一个类似eq({team_id:1,players:{userId:1,userId:21,userId:34}的查询(而不是ID,我希望对象只是其示例。
getRepository(Team).createQueryBuilder("team")
.leftJoin("team.users", "userTeam")
.leftJoin("userTeam.user", "user")
.getMany()
这使我得到QUERY:
SELECT "team"."id" AS "team_id", "team"."name" AS "team_name", "team"."shortcut" AS "team_shortcut", "team"."nationality" AS "team_nationality", "team"."description" AS "team_description", "team"."imagePath" AS "team_imagePath", "team"."division" AS "team_division" FROM "team" "team" LEFT JOIN "user_team" "userTeam" ON "userTeam"."userId"="team"."id" LEFT JOIN "user" "user" ON "user"."id"="userTeam"."userId"
问题在那里,因为我有
LEFT JOIN "user_team" "userTeam" ON "userTeam"."userId"="team"."id"```
代替
userTeam.teamId=team.id
您对解决我的问题有任何想法吗?我会很高兴收到任何建议和提示。
答案 0 :(得分:0)
问题已解决。我有相反的关系。我应该有
@OneToMany(type => UserTeam, userTeam => userTeam.user)
teams = undefined;
@OneToMany(type => UserTeam, userTeam => userTeam.team)
users = undefined;