Can two fields reference the same field on another model - prisma

I have two models in my prisma schema for representing a sort of game schedule where 2 "players" can play against each other in a "matchup":
model Matchup {
id Int #id #default(autoincrement())
homePlayer Player #relation("Home Player", fields: [homePlayerId], references: [id])
homePlayerId Int
awayPlayer Player #relation("Away Player", fields: [awayPlayerId], references: [id])
awayPlayerId Int
}
model Player {
id Int #id #default(autoincrement())
homeMatchUps Matchup[] #relation("Home Player")
awayMatchUps Matchup[] #relation("Away Player")
}
I don't necessarily need to have the distinction of a "home" player and an "away" player however. I'd rather that the Player model have a single reference to any matchup they were associated with, as opposed to having to split it between two fields.
What would be the correct way to approach this?

It's not possible to have a single reference to Matchup from the Player side, you need to have two. So what you're asking for can't really be done.
The reason is, homePlayer and awayPlayer represent two separate one-to-many relations and you need to have a matching field in the many side (in this case Player) for every single one-to-many relation.

Related

enum in prisma instead of table

I have two tables in my Prisma db. One is for admins and the other is for users, and since they differ a lot from each other I decided not to create an isAdmin field. They have both in common the establishment that is described as a table (one-to-many relation), but it has only one field (name). I'd like to know whether it is a good idea to create an Enum to represent this field. My only doubt is that, since the values of the enums can change (adding more fields dynamically), it is not as good as creating a table
model Admin {
id String #id #default(uuid())
pin Int
establishment Establishment #relation(fields: [establishmentId], references: [id])
establishmentId Int
}
model User {
id String #id #default(uuid())
name String
balance Float #default(0)
establishment Establishment #relation(fields: [establishmentId], references: [id])
establishmentId Int
}
model Establishment {
id Int #id #default(autoincrement())
name String
}
This is how I've imagined the new data source to be like
model Admin {
id String #id #default(uuid())
pin Int
establishment Establishment
}
model User {
id String #id #default(uuid())
name String
balance Float #default(0)
establishment Establishment
}
enum Establishment {
ESTABLISHMENT1
ESTABLISHMENT2
}
If you are sure that you won't need to store any more fields for Establishment then the enums approach is good. But in case in future you are envisioning that there could be other common fields, then you would need to duplicate them in Users and Admins Table and Enums approach won't be a fit.
Another approach could be to use the check constraint on database level to only allow two valid values i.e. ESTABLISHMENT1 and ESTABLISHMENT2, this way you won't even need to define enums.

Relation two to many with Prisma

I'm trying to create a prisma schema with this kind of relation - I have two models: Player and Game. Every game is "played" by two players and I'm trying to connect them with something like this:
model Player {
id String #id #default(cuid())
name String
games Game[]
}
model Game {
id Int #id #default(autoincrement())
player_1 Player
player_2 Player
winnerId String
}
In this case I get an error which says Ambiguous relation detected. The fields player_1 and player_2 in model Game both refer to Player. Please provide different relation names for them by adding #relation(<name>).
And if I add relation with names which looks like this:
player_1 Player #relation(name: "GamePlayer1")
player_2 Player #relation(name: "GamePlayer2")
I get an error The relation field player_1/player_2 on Model Game is missing an opposite relation field on the model Player. I don't think that adding additional fields to Player model is necessary and also don't think that adding many to many relation which would look like this players: Player[] is smart.
What would be the right way to connect those two models based on my example?
You need to provide both player's id on Game and separate relation name on Player and Game. Here the right way to fix it:
model Player {
id String #id #default(cuid())
name String
games_player_1 Game[] #relation(name: "GamePlayer1")
games_player_2 Game[] #relation(name: "GamePlayer2")
}
model Game {
id Int #id #default(autoincrement())
player_1_id String
player_2_id String
player_1 Player #relation(name: "GamePlayer1", fields: [player_1_id], references: [id], onDelete: Cascade)
player_2 Player #relation(name: "GamePlayer2", fields: [player_2_id], references: [id], onDelete: Cascade)
winnerId String
}

What is the best way to model a one-to-many relationship that also has a "selected" object in Prisma?

In our app, a user can be a member of many organizations. While viewing the site, they are generally viewing it through one of those organizations. This is the selected organization.
Is the following model the best way to model that? Is there a way to model it so you don't have to have the selectedBy and selectedById on the Membership and just have the selectedMembershipId on the User with a foreign key to the Membership?
model User {
id Int #id #default(autoincrement())
memberships Membership[] #relation("Memberships")
selectedMembership Membership? #relation("Selection")
}
model Membership {
id Int #id #default(autoincrement())
user User #relation("Memberships", fields: [userId], references: [id])
userId Int
selectedBy User? #relation("Selection", fields: [selectedById], references: [id])
selectedById Int? #unique
organization Organization #relation(fields: [orgId], references: [id])
orgId Int
role MemberRole
##unique([userId, orgId])
}
It's not possible to do precisely what you're hoping to. The syntax for relations in prisma calls for having a relation field (To the best of my understanding, that's what you would prefer not to have in your schema).
Since it's a one-to-one relation, if you prefer, you could do it the other way around, and have a selectedMembershipId foreign key on the User side and only have the selectedBy relation field on the Membership side like this:
model User {
id Int #id #default(autoincrement())
memberships Membership[] #relation("Memberships")
selectedMembership Membership? #relation("Selection", fields: [selectedMembershipId], references: [id])
selectedMembershipId Int? #unique
}
model Membership {
id Int #id #default(autoincrement())
user User #relation("Memberships", fields: [userId], references: [id])
userId Int
selectedBy User? #relation("Selection")
...
}
However, this is really a matter of preference regarding which side to keep the foreign key. In my opinion, the way you're handling your schema is the most reasonable way to model the relationship in Prisma.

Prisma schema - Create a relation field from multiple possible foreign keys (OR relation)

How to create a relation field in prisma schema that target 2 possible relation scalar fields ?
For instance, in soccer, let's say we've got the following 2 models:
model Match {
team1Id Int
team1 Team #relation("team1", fields: [team1Id], references: [id])
team2Id Int
team2 Team #relation("team2", fields: [team2Id], references: [id])
}
model Team {
id Int #default(autoincrement()) #id
name String
matches Match[] #relation( /* What to put here ? */ ) // <----
}
What to put in the Team.matches relation field definition in order to allow Team.matchs to contain any match the team played from any side, as team1 or as team2 ?
This is not possible in Prisma right now! I created an issue in our repo for technical specifications to think about ways for improving this!
Workaround
With Prisma you always need to have a relation field on both sides per relation. This means you need to have two relation fields on Team, one which represents the matches where the team played "as team 1", another where it played "as team 2".
model Match {
team1Id Int
team1 Team #relation("team1", fields: [team1Id], references: [id])
team2Id Int
team2 Team #relation("team2", fields: [team2Id], references: [id])
##id([team1Id, team2Id])
}
model Team {
id Int #default(autoincrement()) #id
name String
matchesAsTeam1 Match[] #relation("team1")
matchesAsTeam2 Match[] #relation("team2")
}

Entity Framework add assocations to many to many by key only

Suppose you have one of the simplest text book models:
Product {
Categories
}
Where a product can be associated to 0 to many categories.
Suppose category looks similar to
Category {
int Id { get; set; }
string Name {get; set; }
}
Now I want to associate my product to (existing) categories with ids 1, 2, and 3. Is there anyway to create this association without loading categories 1, 2, and 3 into memory?
I know this would be possible with a single Category where my Product model had Category and CategoryId on it. Is there a similar convention for bonding multiple items?
To reiterate, my purpose is to avoid querying categories. I already have the identifiers. With direct sql I could easily establish these relationships by key only (in fact the association table is literally just the keys). I want to know if there's an "entity framework way" of doing this or whether direct sql is the only option.
You could create category instances with just the id and attach them to the context. Then you could add to the product, without having to pull the categories from the database.
For example:-
var category = new Category { Id = 1 };
db.Categories.Attach(category);
product.Categories.Add(category);
db.SaveChanges();