-
QuestionHi. I'm trying to set up a schema (for a PostgreSQL database) where two records in a table can either connected or not. I'm thinking of something like this: (the below is not a valid schema) But since Prisma always requires opposite relations (see #21380 ), I need to add two very confusing fields So the best I have come up with is this: which seems very complex for a seemingly simple contruct. My question is whether there is a simpler or more comprehensible way to configure this? Information about Prisma Schema, Client Queries and Environment (optional)
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
Hi @foolo 👋 Have you considered modeling the relationships using a Many-to-Many Self relation. For example: model User {
id Int @id @default(autoincrement())
username String @unique
friends User[] @relation("UserFriends")
friendsOf User[] @relation("UserFriends")
}This relation expresses the following:
This many-to-many-relation is implicit. If you need the relation to hold other fields, you can create an explicit many-to-many self relation as well. |
Beta Was this translation helpful? Give feedback.
This will work for Relational Databases (Postgres, Mysql etc). Do take a look at the documentation.