-
QuestionGood morning, I have a project that I am developing that needs to deal with multiple databases, initially two, both Sql Server: Database 1: Server=database1;Database=tesla1;User Id=sa;Password=mysecret;TrustServerCertificate=true; and needed to create schemas for both to create their tables. How could I do that? So when I do:
Create the tables in their respective database. Would a command like this work? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
Hi @rdrigos 👋 To work with multiple databases, you can create separate schema files for each database. In each schema file, you define the datasource with the respective database connection string. Here's an example of how you can set up your schema files: datasource db {
provider = "sqlserver"
url = env("DATABASE_URL_DB_1")
}
generator client {
provider = "prisma-client-js"
output = "../../node_modules/@prisma-db-1/client"
} For Database 2, create a schema file datasource db {
provider = "sqlserver"
url = env("DATABASE_URL_DB_2")
}
generator client {
provider = "prisma-client-js"
output = "../../node_modules/@prisma-db-2/client"
} In your .env file, you should define the
In your code, you can import the generated clients like this: import { PrismaClient as PrismaClient1} from '@prisma-db-1/client';
import { PrismaClient as PrismaClient2} from '@prisma-db-2/client'; This way, Currently, Prisma does not support running a single migrate command for multiple schemas. You need to run the migrate command separately for each schema. |
Beta Was this translation helpful? Give feedback.
Hi @rdrigos 👋
To work with multiple databases, you can create separate schema files for each database. In each schema file, you define the datasource with the respective database connection string. Here's an example of how you can set up your schema files:
For Database 1, create a schema file
prisma/db-1/schema.prisma
:For Database 2, create a schema file
prisma/db-2/schema.prisma
: