Jessica
Jessica

Reputation: 9830

Prisma throwing error: Ambiguous relation detected

I'm using prisma and trying to model a referral table for my Postgres database. Not sure if the db schema is correct, but I have a table with a referId, userId: 1 to 1, and referredUserId: 1 to many.

model Referral {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now())

  referId String // Auto Generate Random String

  userId Int  @unique
  user   User @relation(fields: [userId], references: [id])

  referredUsersId Int[]
  referredUsers   User[] @relation(fields: [referredUsersId], references: [id])
}

I'm not sure exactly how to reference these in the User model. I tried

Referral        Referral? 
UsersReferred Referral[]

But I get an error

Error validating model "User": Ambiguous relation detected

What's the correct way to model a referral table and how can I do it in prisma?

Upvotes: 3

Views: 1825

Answers (1)

Nurul Sundarani
Nurul Sundarani

Reputation: 7558

There are multiple relations between the same two models so you would need to specify the name for the relations.

model User {
  id            String     @id
  Referral      Referral?  @relation("UserReferral")
  UsersReferred Referral[] @relation("ReferredUsers")
}

model Referral {
  id              Int      @id @default(autoincrement())
  createdAt       DateTime @default(now())
  updatedAt       DateTime @default(now())
  referId         String
  userId          String   @unique
  user            User     @relation(fields: [userId], references: [id], name: "UserReferral")
  referredUsersId String
  referredUsers   User     @relation(fields: [referredUsersId], references: [id], name: "ReferredUsers")
}

Here's the reference for Disambiguating relations: Reference

Upvotes: 7

Related Questions