carlos3g / echoes

Platform for sharing quotes, like pensador.com
0 stars 0 forks source link

feature: polymorphic relationships #1

Open carlos3g opened 2 weeks ago

carlos3g commented 2 weeks ago

https://github.com/prisma/prisma/issues/1644 https://github.com/prisma/prisma/discussions/11108 https://stackoverflow.com/questions/69224864/polymorphism-in-prisma-schema-best-practices https://github.com/keinsell/is-prisma-production-ready

carlos3g commented 2 weeks ago

This solution doesn't work as explained here.

Models tested:

model Quote {
  id                BigInt              @id @default(autoincrement()) @db.BigInt
  userOnFavoritable UserOnFavoritable[] @relation("UserOnQuote")

  @@map("quotes")
}

model Author {
  id                BigInt              @id @default(autoincrement()) @db.BigInt
  userOnFavoritable UserOnFavoritable[] @relation("UserOnAuthor")

  @@map("authors")
}

model UserOnFavoritable {
  user            User            @relation(fields: [userId], references: [id])
  userId          BigInt          @db.BigInt
  author          Author?         @relation("UserOnAuthor", fields: [favoritableId], references: [id], map: "author_favoritableId")
  quote           Quote?          @relation("UserOnQuote", fields: [favoritableId], references: [id], map: "quote_favoritableId")
  favoritableId   BigInt          @map("favoritable_id") @db.BigInt
  favoritableType FavoritableType @map("favoritable_type")

  @@unique([userId, favoritableId, favoritableType])
  @@map("user_on_favoritable")
}

Error:

Foreign key constraint failed on the field: `author_favoritableId (index)`
[...]
{
  code: 'P2003',
  clientVersion: '5.18.0',
  meta: { modelName: 'Quote', field_name: 'author_favoritableId (index)' }
}