New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support querying across multiple schemas #1122
Comments
Hi @pantharshit00 do you have any update about this issue? |
Sorry, there are no updates on this right now. |
@pantharshit00 are there any plans to support this going forward? I might be misunderstanding things but is my understanding correct that Prisma currently only supports connecting to a single PostgreSQL schema? In our database, we have tables and views split across multiple schemas that we need to be able to pull data from. We really want to use Prisma, but until there is support for connecting to multiple schemas in the same DB, it will not be possible for us to do so. |
+1 for that. This item has been open for too long. I too use multiple schemas in a single project and in order to migrate completely to Prisma, this is a must. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Hi @ruheni, I wanted to try Prisma at one project. I had a multi-tenant application and each tenant(user) has a few tables in his schema. eg.: schema: tenant_1 then we had a "shared" schema which contained only 1 table When user_1 signs in he has read/write access to his tenant_1 schema and read access for shared schema. At the moment solution for this is to create 2 Prisma instances one connected to tenant_1 schema and the second instance connected to shared schema. I think this solution is a little bit hacky and also I'm not able to do joins. Thank you 😃 |
+1 for this, a pretty important use case for us. |
I'm using different schemas to organize data. For example, I have the geo schema which includes IP mapping tables, gelocations etc that I'm sharing with multiple projects/APIs. |
I have a similar use case. On some projects we leverage multiple schemas to organize the data. For example, one schema may be used to house all tables and views associated with reporting while another may be used to house all tables and views associated with data importers. Thanks much! |
I too need this. I have separate postgres schemas for each customer. |
any update about this? |
I needed to query across two schemas today and was able to use db.$queryRaw`
SELECT a.*
FROM "Activity" a
JOIN public."Member" m on m.id = a."memberId"
JOIN auth.users u on m.email = u.email
WHERE m.email = ${context.currentUser.email}
ORDER BY a."occurredAt" desc Here I join member activity (from Worked great. |
Gotta say that this is a major bummer as we've designed a database with multiple schemas. This definitely should be implemented sometime and yeah, looks like raw queries are the only way of going about it for now. Edit: Perhaps instead of creating a Prisma connection straight to the schema via URL, make it to the database and allow the user to choose the schema? So something on the lines of: prisma.schema('example_organisation').users.findUnique({ where: ... and if the schema does not exist, throw an error that could be handled by the dev, e.g. 'Schema not defined'? Lets say an organisation uses a schema for each of their clients and each schema looks exactly the same i.e. it has the same tables with the same constraints, relations etc. It would be nice if we could define a e.g. customer.prisma schema and tell prisma that we wish to use that schema when dealing with certain schemas in out database, perhaps via something like prisma.schemas({ dbSchema: 'example_organisation', prismaSchema: 'customer' }).users.findUnique({ where: ... though there could be an issue with intellisense and type-checking during coding. Just thought I'd float some ideas. |
To me, this seems to be violating the rules of microservices, such as "no shared data context", and "discrete boundaries". This is bound to lead to development issues down the line as I have seen time and time again. So in my personal opinion, this feature should not have high priority. You should be able to create a Prisma schema/client per db schema. If you are using foreign key relations across database schemas, you DEFINITELY are violating the principles of microservices. |
Though not everyone uses it for microservices; for instance, MS SQL environments often use schemas for security/access controls with each schema having different CRUD permissions for different clients/credentials. In this case it has nothing to do with separation of concern and just the restrictions a table needs. |
Could you have it as db.user.findMany({ Then it would not be a breaking change? |
Hello, I'm having an issue using the
However, when I change the
I'm using this as a workaround to copy the generated prisma models and then run |
Whoah, that is super weird @danielwarke. Can you open a standalone issue, fill all the fields of the bug template, so we can start digging into this? Thanks. |
|
Would love to have the feature available on mongodb |
I am looking forward to getting schema support to be able to isolate schemas from each other to support modular monoliths and micro services that use the same database host. I'd also love different schemas to be able to have tables with the same name. I'd also love for the different schemas to be defined in different folders mixed in my code. If this isn't supported, there are ways to manually prefix all tables with the schema name, and then isolate the schema by using TypeScript Pick<> type helper to pick out the schema models manually. |
Hm, I am not sure if we have a proper issue for the approach you are describing @marcusradell - we have this one for cross schema queries and relations, where one app just stores its data in multiple schemas. And we have #12420, where the same Prisma schema should be deployed to different database schemas to achieve isolation of the data. Can you please open a new feature request and describe your use case a bit? Thanks. |
That would be amazing, because that would allow to decide what schema to use at runtime. Someone already mentioned a long time ago in this thread a use case that we have as well, which is having customer data separated by database schema. The tables in these account schemas are the same, so we could be doing something like: prisma.model.findUnique({ where: {...}, schema: customerSchema }) Right now there's no way to use Prisma for such DB design. |
This is a much better solution thank you! |
Any update for mysql? |
Not at this time @conioX, we'll post an update here when we start work on it. |
Haven't tried to create migrations files with |
@janpio At the risk of repeating a question often asked (though it seems not for the past few months or so). Is there a plan (and possibly schedule) to add this for MySQL? Right now we're using 8 different Prisma connections to all our databases but it's quite unwieldy, and we cannot query cross database. |
@gruckion not sure how you came up with that piece of genius. Was about to mess with multiple schema bs, but using views is just beautiful. Much appreciated |
How do you reference it in other tables tho? Seems like it would be bottleneck since views are just saved queries |
I don't, in my usecase I just needed a joined table for which the view solves it really nice |
Sorry to jump into a long thread. Can anyone help me determine if there is a way to rename a database schema in the prisma migration files? |
I am just getting into building multi-tenant apps. I plan is to isolate tenants with schemas. There is a shared schema where a new tenant's data is stored, a script then runs on insert to create a schema for that tenant. A subdomain would be issued to the tenant and it would be the same as their schema name. Having a way to select which schema to query from would be nice. |
Right now with Prisma, you can query across one schema at a time but in many cases, you need to query across multiple tables stored across multiple database schemas.
We need to make certain product decision to enable this like how we are going to add this schema metadata information to the model and other implementation details that are necessary to be handled for this.
The text was updated successfully, but these errors were encountered: