Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
Prisma following/follower relationship schema

I have a user model, and i want to add following/followers system, and for that i think i need to create a separate table that looks like this

id | user_id | follower_id
1  | 20      | 45
2  | 20      | 53
3  | 32      | 20

but i have no idea how to create the schema for that, what I've done is this:

model User {
  id         Int         @id @default(autoincrement())
  username   String
  Follows    Follows[]
}

model Follows {
  id           Int      @id @default(autoincrement())
  following_id Int?
  follower_id  Int?
  user_Following    User     @relation(fields: [following_id], references: [id])
  user_Follower     User     @relation(fields: [follower_id], references: [id])
}

but that of course doesn't work and is giving me an error

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here's the way I'd suggest modeling your schema.

model User {
  id        String  @id @default(autoincrement())
  username  String
  followers Follows[] @relation("following")
  following Follows[] @relation("follower")
}

model Follows {
  follower    User @relation("follower", fields: [followerId], references: [id])
  followerId  String
  following   User @relation("following", fields: [followingId], references: [id])
  followingId String

  @@id([followerId, followingId])
}

Changes

  1. User table has two relation fields instead of one.
  2. followerId and followingId are made mandatory. It doesn't really make sense to have a Follows relation table when either of those are absent. (You can't have a following relationship without one user following and one user to follow).
  3. @@id([followerId, followingId]) represents the primary key in Follows table. A separate id field is redundant.
  4. Changed field names to camelCase, which is the recommended convention in Prisma.

4 is optional ofcourse, but I'd suggest following it none the less.

You can find more details about this in the many-to-many subsection of the self-reation article in the Prisma doc.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda