Muhammad Rehan Qadri
Muhammad Rehan Qadri

Reputation: 7654

Prisma: Query across multiple schemas in a database

Does prisma support the ability to fetch data from multiple schemas from within a single database?

Upvotes: 9

Views: 6536

Answers (2)

shmuels
shmuels

Reputation: 1393

Prisma multiSchema is now supported as a preview feature.

See here https://www.prisma.io/docs/guides/database/multi-schema

It was introduced in version 4.3.0 https://github.com/prisma/prisma/issues/1122#issuecomment-1231773471

As the docs say you would add the preview feature...

generator client {
  provider = "prisma-client-js"
  previewFeatures = ["multiSchema"]
}

Then in your datasource you note the schemas...

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
  schemas  = ["schema1", "schema2"]
}

And finally in each model you add the @@schema attribute...

model User {
  id      Int      @id
  orders  Order[]
  profile Profile?

  @@schema("schema1")
}

model Order {
  id      Int  @id
  user    User @relation(fields: [id], references: [id])
  user_id Int

  @@schema("schema2")
}

Upvotes: 0

Alex Ruheni
Alex Ruheni

Reputation: 1169

That is not yet possible. Here's a GitHub issue that had been created you can use to monitor the feature's status and give your feedback too.

Could you also share your use case too? Your feedback will be highly appreciated as we relay it back to the team. 🙂

Upvotes: 7

Related Questions