Hello Guys, I have a question.. I need to create s...
# orm-help
s
Hello Guys, I have a question.. I need to create some kind of relation between 3 tables (or types) - User, Team and Role. The case is -> User could create the team, if team is created user get role “capitan”, user, could create multiple teams. Also user could be added to another team, by other User (capitan) in these case, user gonna have role “player”. So, user can be part of multiple teams, with only one role in single one: “UserA, TeamA, “Capitan”, “UserA, TeamB, “Player”, I just tried to find answer in google, but i can not find any solution for this case. Is this possible to create relation like this in Prisma?
k
I'm not sure your really need a Type for roles. A user could have a field teamsAsCaptain and an another one teamsAsPlayer. Both are arrays of Team. A Team could have a field captain which is an object User and a field players which is a array of Users.
Then, in the application, when a user create a team, he will be the captain and when he adds other users, they are all players
What I mean is that the role is a property, not a object.
s
This is some kind of solution, but only for short time. What if I’d like to add another third role for user? Then I need to add another array.. This solution doesn’t protect database, it’s only work from application side. because I still can have duplicated objects in database. This isn’t long term solution in my opinion.. But thanks for your advice.
a
why don’t you add the team information to the role entity. A user than has a list of roles and list of teams
l
User -> UserTeam -> Team. This way the role is a property of UserTeam, the User can have many UserTeams
s
@lancej so it should looks like: type User { id .... teams: [UserTeam!]! } type Team { id... players: [UserTeam!]! } type UserTeam { user: User! team: Team! role: Role! } ??
l
Something like that. Could have another model for players, players: [TeamPlayer!]!. A TeamPlayer then can have specific info about that player on that team. TeamPlayer then can point to User
s
I will check that, but I'm not sure if this solution avoid elements duplication. I mean user can be in one team twice..