Seems like the schema generic is missing - did you forget to add it to your DB type?
Hi, I am trying to use Drizzle in a multischema setup. The docs say to make the config and then create the db adapter. I would assume that the folder I set as schema in drizzle.config.ts would automatically pass it to the db client. But I get . Do I have to explicitly define all the schemas I'm using in the db client like: export const database = drizzle(sqlite, {schema1, schema2}) , or is there something wrong? The way it seems in the setup for the docs, all I need is to define the schema folder and the db() should gather the types from it automatically.
7 Replies
Pls
you need to pass the schema in you
drizzle()
init
for each schema I suppose, not sure how it would work otherwiseI figured when you defined it in the config that was good enough for the types to populate, because the get started docs never mention to add the schema to the drizzle() init
So I’m not really 100% sure on how to do it with multiple schemas, but I could prolly just make a barrel file or something
I think the config file is primarily for migrations. You still need to pass it to the
drizzle()
init for runtime code.Gotcha thanks that seems right, it’s weird they don’t have you do that in the docs
im having the same exact issues as you, have you been able to fix it?
You don't need to pass any schemas to the
drizzle()
function in order to use the query builder. With the query builder functions, you're already passing the table schema to those functions, e.g. db.select().from(myTableSchema)...
so it can get everything it needs from there. If one of those tables has a foreign key to a table in a non-default schema though, you will have to define that with pgSchema
somewhere and define enough of the foreign table to use it, e.g...
In my current project where I serve all data through a JSON Server style API (where the client just asks for related records when necessary) I have this in my functions/db/main/connect.ts
file:
Supplying the schema to drizzle-kit for migrations via drizzle.config.ts
is a completely different thing, even though drizzle()
can use the same table-schemas. Here are my notes for my functions/db/main/schema.ts
file:
I think there's also a mode for drizzle where the runtime drizzle()
instance can pickup settings out of drizzle.config.ts
but I don't use that mode, I just use the drizzle query builder functions and I pass my table schemas directly to those e.g. db.select().from(myTableSchema)...