在Prisma中,我想为以下模型建模,但我不确定如何实现。
type Event {
id: ID! @unique
players: [User]! @relation(name: "EventPlayers")
teams: [Team]! @relation(name: "EventTeams")
...
}
type User {
id: ID! @unique
eventsPlayed: [Event]! @relation(name: "EventPlayers")
...
}
type Team {
id: ID! @unique
event: Event! @relation(name: "EventTeams")
members: [User]! @relation(name: ?????)
...
}
team member
必须位于Event.players
Event.player
只能分配给一个(或没有)团队我觉得我在这里需要多对多的关系,但是我很难解决这个问题。 Team.members
与?????有什么关系?我什至正确地解决了这个问题?
我打算创建一个用于创建团队的拖放界面。在Events.players
中列出但尚未分配为Team.member
的用户将在unassigned
存储桶中。将它们拖到团队中,将其分配为Team.member
。但是,我想将所有玩家查询为events { players { id }}
和events { teams { members { id }}}
经过深思熟虑之后,我正在考虑解决该问题的另一种方法。这是一个更新的架构,我很乐意您的想法/投入。
type Event {
id: ID! @unique
users: [EventUser!]!
teams: [Team!]!
title: string
}
type EventUser {
event: Event!
user: User!
role: EventRole!
}
type User {
id: ID! @unique
events: [EventUser!]!
name: string
}
type Team {
event: Event!
members: [EventUser!]!
name: string
}
enum EventRole {
ADMIN
COORDINATOR
JUDGE
PLAYER
REVIEWER
SPONSOR
}
答案 0 :(得分:1)
您的大多数模型都是准确的。您需要做一些小的更改:
@relation
伪指令:仅当关系不明确时才需要。例如,自我关系。您不需要这种情况。这是ORM思维的传统方式。players: [User]!
关系。它说玩家字段不能为空。它必须是列表。但这使User
为可选,这意味着您可以拥有players = [user1, null, user2]
。您可能不想要这个。这几乎适用于所有多对关系。User
类型应具有一个team
字段,该字段是可选的。会有一些用户不属于任何团队。通过上述调整,您的架构将如下所示:
type Event {
id: ID! @unique
# Note DOUBLE EXCLAMATION
# Ensure that User, as well as players, are not null.
players: [User!]!
# Note DOUBLE EXCLAMATION
# Ensure that Team and teams are not null.
teams: [Team!]!
}
type User {
id: ID! @unique
eventsPlayed: [Event!]!
team: Team
}
type Team {
id: ID! @unique
event: Event!
members: [User!]!
# Added an extra key for keeping track of past events if required
pastEvents: [Event!]!
}