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
Seems like the schema generic is missing - did you forget to add it to your DB type?
Seems like the schema generic is missing - did you forget to add it to your DB type?
. 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.
import { defineConfig } from "drizzle-kit";

export default defineConfig({
schema: "./src/db/schema",
out: "./src/db/migrations",
dialect: "postgresql",
dbCredentials: {
url: process.env.DATABASE_URL || "",
},
});
import { defineConfig } from "drizzle-kit";

export default defineConfig({
schema: "./src/db/schema",
out: "./src/db/migrations",
dialect: "postgresql",
dbCredentials: {
url: process.env.DATABASE_URL || "",
},
});
7 Replies
salzar
salzarOP5mo ago
Pls
spdermn
spdermn5mo ago
you need to pass the schema in you drizzle() init for each schema I suppose, not sure how it would work otherwise
salzar
salzarOP5mo ago
I 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
Sillvva
Sillvva5mo ago
I think the config file is primarily for migrations. You still need to pass it to the drizzle() init for runtime code.
salzar
salzarOP5mo ago
Gotcha thanks that seems right, it’s weird they don’t have you do that in the docs
!CANDY
!CANDY3mo ago
im having the same exact issues as you, have you been able to fix it?
JustWayne
JustWayne3mo ago
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...
/** The built-in Supabase schema. */
export const auth = pgSchema("auth");
/** The built-in Supabase `auth.users` table. */
export const users = auth.table("users", {
id: uuid("id").primaryKey(),
});
/** The built-in Supabase schema. */
export const auth = pgSchema("auth");
/** The built-in Supabase `auth.users` table. */
export const users = auth.table("users", {
id: uuid("id").primaryKey(),
});
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:
import { neon } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
// Local
import { env } from "@/lib/env";
// import * as schema from "./schema";

const client = neon(env.NEON_DB_URL);

export const mainDb = drizzle({
client,
// CONSIDER: Supply schema when relational features are needed.
// schema,
});
export type MainDb = typeof mainDb;
import { neon } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
// Local
import { env } from "@/lib/env";
// import * as schema from "./schema";

const client = neon(env.NEON_DB_URL);

export const mainDb = drizzle({
client,
// CONSIDER: Supply schema when relational features are needed.
// schema,
});
export type MainDb = typeof mainDb;
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:
/**
* @file Drizzle schema
*
* REQUIREMENTS:
*
* - Export only the schema types that Drizzle-ORM and Drizzle-Kit use.
* - Non-Drizzle models/types/schemas/etc should be defined elsewhere, e.g.
* in the functions/models/ directory.
*
* GUIDELINES:
*
* - Export tables/views/functions/etc in order of least dependencies first,
* which should align with the order they'll be created in the database.
*/

export * from "./tables/table_1";
export * from "./tables/table_2";

export * from "./tables/table_3";
export * from "./tables/...";
/**
* @file Drizzle schema
*
* REQUIREMENTS:
*
* - Export only the schema types that Drizzle-ORM and Drizzle-Kit use.
* - Non-Drizzle models/types/schemas/etc should be defined elsewhere, e.g.
* in the functions/models/ directory.
*
* GUIDELINES:
*
* - Export tables/views/functions/etc in order of least dependencies first,
* which should align with the order they'll be created in the database.
*/

export * from "./tables/table_1";
export * from "./tables/table_2";

export * from "./tables/table_3";
export * from "./tables/...";
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)...

Did you find this page helpful?