Multiple Schema | is not assignable to type 'PostgresJsDatabase'.
So guys, im trying to move to Drizzle
so, i already have my database on postgres, and pull it to make its migration and schema using drizzle-kit
here's my drizzle.config.ts
import dotenv from "dotenv";
import type { Config } from "drizzle-kit";
dotenv.config();
export default {
schema: "./src/schema/*.ts",
out: "./drizzle",
driver: "pg",
dbCredentials: {
connectionString: process.env.DATABASE_URL ?? "",
},
} satisfies Config;
5 Replies
then i execute the introspect:pg command, and it works properly, it creates a .sql and schema.ts file
however, the schema generated is single file only inside /rootProject/drizzle directory
so i split the file manually into /rootProject/src/schema directory
then i make one index.ts inside that directory file to export all the .ts schema file
here's the code of index.ts file
export * from "./chain";
.....
.....
and here's one example of schema that i split
chain.ts
import {
integer,
pgTable,
serial,
text,
uniqueIndex,
} from "drizzle-orm/pg-core";
export const Chain = pgTable(
"Chain",
{
id: serial("id").primaryKey().notNull(),
name: text("name").notNull(),
chainId: integer("chainId").notNull(),
symbol: text("symbol").notNull(),
},
(table) => {
return {
chainIdKey: uniqueIndex("Chain_chainId_key").on(table.chainId),
};
}
);
now, i want to create the DB instance using my DB Class, but it gives me an error
something like this
Type 'PostgresJsDatabase<{ Chain: PgTableWithColumns<{ name: "Chain"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "Chain"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, {}, {}>; name: PgCo...' is not assignable to type 'PostgresJsDatabase'.
The types of '_.schema' are incompatible between these types.
Type 'ExtractTablesWithRelations<{ Chain: PgTableWithColumns<{ name: "Chain"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "Chain"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, {}, {}>; na...' is not assignable to type 'ExtractTablesWithRelations<Record<string, never>>'.
Property 'Chain' is incompatible with index signature.
Type '{ tsName: "Chain"; dbName: "Chain"; columns: { id: PgColumn<{ name: "id"; tableName: "Chain"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, {}, {}>; name: PgColumn<...>; chainId: PgColumn<...>; symbol: PgCo...' is not assignable to type '{ tsName: string; dbName: never; columns: never; relations: Record<string, Relation<string>>; primaryKey: AnyColumn[]; }'.
Types of property 'dbName' are incompatible.
Type 'string' is not assignable to type 'never'.ts(2322)
(property) DrizzleInstance.instance: PostgresJsDatabase
and here's the DB Initialization code
import { drizzle, PostgresJsDatabase } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "../schema";
class DrizzleInstance {
private static instance: PostgresJsDatabase | null = null;
private static queryClient = postgres(process.env.DATABASE_URL);
public static get Instance(): PostgresJsDatabase {
return (
this.instance ||
(this.instance = drizzle(this.queryClient, { <---- this line throws error on my vscode
schema: { ...schema },
}))
);
}
}
const DrizzleDB = DrizzleInstance.Instance;
export default DrizzleDB;
am i missing something ?I'm new to drizzle but I don't think you need to spread the schema there at the end. Just drizzle(client, schema)
i managed to solve it, anyway, i need to provide schema to be able to use .query.table name API
How did you solve it?
just provide the type
import dotenv from "dotenv";
import { drizzle, PostgresJsDatabase } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "../schema";
dotenv.config();
class DrizzleInstance {
private static instance: PostgresJsDatabase<typeof schema> | null = null;
private static queryClient = postgres(process.env.DATABASE_URL);
public static get Instance(): PostgresJsDatabase<typeof schema> {
return (
this.instance ||
(this.instance = drizzle(this.queryClient, {
schema: { ...schema },
}))
);
}
}
const DrizzleDB = DrizzleInstance.Instance;
export default DrizzleDB;