DT
Drizzle Team•5mo ago
Cambaru

Creating DB with schema

I am pretty new to drizzle and try to follow the documentation. I found that you can initiallize drizzle with a schema like so:
import {drizzle} from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import * as schema from './schema';

export const connection = await mysql.createConnection(
process.env.DATABASE_CONNECTION as string
);

export const db = drizzle(connection,{schema});
import {drizzle} from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import * as schema from './schema';

export const connection = await mysql.createConnection(
process.env.DATABASE_CONNECTION as string
);

export const db = drizzle(connection,{schema});
./schema is my MySQL Database schema like so:
import {mysqlTable, varchar, int, boolean, timestamp} from 'drizzle-orm/mysql-core';

export const answers = mysqlTable('answers', {
id: int('id').primaryKey().autoincrement(),
text: varchar('text', { length: 2048 }).notNull(),
correct: boolean('correct').notNull(),
questionId: int('questionId').notNull()
.references(() => questions.id),
});
... many more
import {mysqlTable, varchar, int, boolean, timestamp} from 'drizzle-orm/mysql-core';

export const answers = mysqlTable('answers', {
id: int('id').primaryKey().autoincrement(),
text: varchar('text', { length: 2048 }).notNull(),
correct: boolean('correct').notNull(),
questionId: int('questionId').notNull()
.references(() => questions.id),
});
... many more
But i get this error wich i cannot explain:
TS2345: Argument of type '{ schema: typeof schema; }' is not assignable to parameter of type 'MySql2DrizzleConfig<typeof import("/home/...../lib/db/schema")> | undefined'.
Type '{ schema: typeof schema; }' is not assignable to type 'Omit<DrizzleConfig<typeof import("/home/...../lib/db/schema")>, "schema"> & { schema: typeof import("/home/...../lib/db/schema"); mode: Mode; }'.
Property 'mode' is missing in type '{ schema: typeof schema; }' but required in type '{ schema: typeof import("/home/...../lib/db/schema"); mode: Mode; }'.
TS2345: Argument of type '{ schema: typeof schema; }' is not assignable to parameter of type 'MySql2DrizzleConfig<typeof import("/home/...../lib/db/schema")> | undefined'.
Type '{ schema: typeof schema; }' is not assignable to type 'Omit<DrizzleConfig<typeof import("/home/...../lib/db/schema")>, "schema"> & { schema: typeof import("/home/...../lib/db/schema"); mode: Mode; }'.
Property 'mode' is missing in type '{ schema: typeof schema; }' but required in type '{ schema: typeof import("/home/...../lib/db/schema"); mode: Mode; }'.
I hope someone can help me 🙂
1 Reply
Mykhailo
Mykhailo•5mo ago
Hey @Cambaru! You should specify mode for your schema in drizzle parameters like here: https://orm.drizzle.team/docs/rqb#modes
Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Want results from more Discord servers?
Add your server