PrismaP
Prisma2y ago
1 reply
edarcode

self-relation

Greetings! I am from Colombia. I was reading the doc on self-relations and I have a problem regarding the interpretation of the relationships or rather the fields that are generated from them.
model User {
  id         Int       @id @default(autoincrement())
  name       String?
  followedBy Follows[] @relation("followedBy")
  following  Follows[] @relation("following")
}

model Follows {
  followedBy   User @relation("followedBy", fields: [followedById], references: [id])
  followedById Int
  following    User @relation("following", fields: [followingId], references: [id])
  followingId  Int

  @@id([followingId, followedById])
}

I have based myself on the previous model to make my own model. (capture #1)

then i run my service to create a record where "lora" will follow "testedarcode" i.e. lora is follerId and testedarcode followingId (captura#2)

However, the result is this (screenshot #3)

This is exactly what I want to achieve, but I had to invert the id fields in my schema to achieve this result. If I do it as the doc indicates, the result is inverted and the follower becomes followed, that is, even though "lora" is following "testedarcode" it would come out inverted as if "testedarcode" was following "lora". Why does this happen? Am I reasoning wrong? I have the impression that my schema is wrong, but I don't know what to do or how to interpret the relationships.
image.png
image.png
image.png
Was this page helpful?