DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB typ

Hello, I'm new to this. I'm using mysql + drizzle-orm + typescript in nextjs. I use next.js boilerplate when I use this
db.query.books.findFirst....
db.query.books.findFirst....
ide/typescript will show error like this for books
DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?">
DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?">
for more detail, I create an issue here: https://github.com/drizzle-team/drizzle-orm/issues/2738
No description
8 Replies
sayuto
sayutoOP5mo ago
up
singgih
singgih5mo ago
try to add typeof you schema.
import * as yourSchema from './path-your-schema'
const db = drizze<typeof yourSchema>()
import * as yourSchema from './path-your-schema'
const db = drizze<typeof yourSchema>()
sayuto
sayutoOP5mo ago
@singgih I was defined types like this
import type { MySqlDatabase } from 'drizzle-orm/mysql-core';
import { drizzle as drizzleMysql } from 'drizzle-orm/mysql2';
import { migrate as migrateMysql } from 'drizzle-orm/mysql2/migrator';
import { createConnection } from 'mysql2';
import { PHASE_PRODUCTION_BUILD } from 'next/dist/shared/lib/constants';

import * as schema from '@/models';

import { Env } from './Env';

let connection;
let drizzle: MySqlDatabase<any, any, any>;

if (
process.env.NEXT_PHASE !== PHASE_PRODUCTION_BUILD &&
process.env.NODE_ENV === 'production' &&
Env.DATABASE_URL
) {
connection = await createConnection({
uri: Env.DATABASE_URL,
});

drizzle = drizzleMysql(connection, { schema, mode: 'default' });

await migrateMysql(drizzle, { migrationsFolder: './migrations' });
} else {
const global = globalThis as unknown as { connection: any };

if (!global.connection) {
global.connection = await createConnection({
...
});
}

drizzle = drizzleMysql(global.connection, { schema, mode: 'default' });
await migrateMysql(drizzle, { migrationsFolder: './migrations' });
}

export const db = drizzle;
import type { MySqlDatabase } from 'drizzle-orm/mysql-core';
import { drizzle as drizzleMysql } from 'drizzle-orm/mysql2';
import { migrate as migrateMysql } from 'drizzle-orm/mysql2/migrator';
import { createConnection } from 'mysql2';
import { PHASE_PRODUCTION_BUILD } from 'next/dist/shared/lib/constants';

import * as schema from '@/models';

import { Env } from './Env';

let connection;
let drizzle: MySqlDatabase<any, any, any>;

if (
process.env.NEXT_PHASE !== PHASE_PRODUCTION_BUILD &&
process.env.NODE_ENV === 'production' &&
Env.DATABASE_URL
) {
connection = await createConnection({
uri: Env.DATABASE_URL,
});

drizzle = drizzleMysql(connection, { schema, mode: 'default' });

await migrateMysql(drizzle, { migrationsFolder: './migrations' });
} else {
const global = globalThis as unknown as { connection: any };

if (!global.connection) {
global.connection = await createConnection({
...
});
}

drizzle = drizzleMysql(global.connection, { schema, mode: 'default' });
await migrateMysql(drizzle, { migrationsFolder: './migrations' });
}

export const db = drizzle;
Mario564
Mario5645mo ago
Try this:
let drizzle: MySqlDatabase<any, any, typeof schema>;
let drizzle: MySqlDatabase<any, any, typeof schema>;
sayuto
sayutoOP5mo ago
@Mario564 thank you, you save my project 🤝
Dane
Dane5mo ago
This doesn't seem to work with the planetscale-serverless driver
Mario564
Mario5645mo ago
@dane.stevens The driver shouldn't affect the type of of the query builder. Can you share your setup file to see what could be wrong?
Want results from more Discord servers?
Add your server