Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: DrizzleQueries are failing because of missing schema name (Postgres) #4060

Open
1 task done
maxdollinger opened this issue Feb 3, 2025 · 3 comments · May be fixed by #4061
Open
1 task done

[BUG]: DrizzleQueries are failing because of missing schema name (Postgres) #4060

maxdollinger opened this issue Feb 3, 2025 · 3 comments · May be fixed by #4061
Labels
bug Something isn't working

Comments

@maxdollinger
Copy link

maxdollinger commented Feb 3, 2025

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.39.1

What version of drizzle-kit are you using?

0.30.4

Other packages

No response

Describe the Bug

We use pgSchema for our table definitions like:
export const user = pgSchema('app_one').table('user', { id: integer('id').generatedAlwaysAsIdentity().primaryKey(), name: text('name'), })

When making a query like db.query.user.findMany() the produced sql code looks like this select "id", "name" from "user" which fails because the schema is missing. It should be select "id", "name" from "app_one"."user"

When using the query builder like db.select().from(user) the produced sql is correct select "id", "name" from "app_one"."user"

@maxdollinger maxdollinger added the bug Something isn't working label Feb 3, 2025
@maxdollinger maxdollinger changed the title [BUG]: Missing schema name on sql queries using DrizzleQueries [BUG]: DrizzleQueries are failing because of missing schema name (Postgres) Feb 3, 2025
@maxdollinger
Copy link
Author

maxdollinger commented Feb 3, 2025

Here some minimal code example with PGlite.

import { PGlite } from "@electric-sql/pglite";
import { integer, pgSchema, text } from "drizzle-orm/pg-core";
import { drizzle } from "drizzle-orm/pglite";

const user = pgSchema('app_one').table('user', { id: integer('id').generatedAlwaysAsIdentity().primaryKey(), name: text('name') });

const client = new PGlite();

await client.exec('CREATE SCHEMA "app_one";');
await client.exec('CREATE TABLE IF NOT EXISTS "app_one"."user" ("id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "app_one"."user_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1), "name" text);');

const db = drizzle(client, {
    schema: { user }, logger: {
        logQuery(query, params) {
            console.log(`[Drizzle] ${query} -- [${params.join(',')}]`);
        }
    }
});

// works
await db.select().from(user);

// does not work
await db.query.user.findMany();

@maxdollinger
Copy link
Author

maxdollinger commented Feb 3, 2025

If i add another if clause in buildFromTable in src/pg-core/dialect.ts (line 308)

if (is(table, Table) && table[Table.Symbol.Schema]) {
    return sql`${sql.identifier(table[Table.Symbol.Schema])}.${sql.identifier(table[Table.Symbol.Name])}`;
}

the issue is no longer present, but looking at the change history i can hardly believe this should be the issue.

@Eiley2
Copy link

Eiley2 commented Feb 10, 2025

If someone else is having this issue, you can downgrade drizzle-orm to 0.38.4 and it works just fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants