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]:drizzle-kit generate casing mismatch for pgView #3332

Open
daenash opened this issue Nov 1, 2024 · 2 comments
Open

[BUG]:drizzle-kit generate casing mismatch for pgView #3332

daenash opened this issue Nov 1, 2024 · 2 comments
Labels
bug Something isn't working drizzle/kit priority Will be worked on next

Comments

@daenash
Copy link

daenash commented Nov 1, 2024

What version of drizzle-orm are you using?

0.35.3

What version of drizzle-kit are you using?

0.26.2

Describe the Bug

I'd like to use camelCase in my application side code but have snake_case in the database.

I have the following drizzle.config.ts

import { defineConfig } from "drizzle-kit";

export default defineConfig({
  dialect: "postgresql",
  schema: "src/db/schema/",
  casing: "snake_case",
  introspect: { casing: "preserve" },
  migrations: { prefix: "index" },
  dbCredentials: {
    ssl: false,
    database: process.env.POSTGRES_DATABASE,
    host: process.env.POSTGRES_HOST,
    password: process.env.POSTGRES_PASSWORD,
    port: process.env.POSTGRES_PORT,
    user: process.env.POSTGRES_USER,
  },
});

When I run drizzle-kit generate for the following schema

export const events = pgTable(
  "events",
  {
    id: uuid().primaryKey().default(sql`gen_random_uuid()`),
    email: text().notNull(),
    device: text(),
    reportTime: timestamp({ withTimezone: true }),
  },
  (t) => ({
    latestEvent: index().on(t.email, t.device, t.reportTime.desc()),
  })
);

export const latestDistinctEventsView = pgView("latest_distinct_events").as(
  (qb) =>
    qb
      .selectDistinctOn([events.email, events.device])
      .from(events)
      .orderBy(events.email, events.device, desc(events.reportTime))
);

I get this SQL

CREATE TABLE IF NOT EXISTS "events" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"email" text NOT NULL,
	"device" text,
	"report_time" timestamp with time zone
);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "events_email_device_report_time_index" ON "events" USING btree ("email","device","report_time" DESC NULLS LAST);--> statement-breakpoint
CREATE VIEW "public"."latest_distinct_events" AS (select distinct on ("events"."email", "events"."device") "id", "email", "device", "reportTime" from "events" order by "events"."email", "events"."device", "events"."report_time" desc);

The issue I'm facing is that the reportTime in the generated SQL is in camel case instead of snake case

CREATE VIEW "public"."latest_distinct_events" AS (select distinct on ("events"."email", "events"."device") "id", "email", "device", "reportTime" from "events" order by "events"."email", "events"."device", "events"."report_time" desc);

Note: Although the order by is correct, the selection is wrong

Expected behavior

I expect the casing to keep the snake case for the view

CREATE VIEW "public"."latest_distinct_events" AS (select distinct on ("events"."email", "events"."device") "id", "email", "device", "report_time" from "events" order by "events"."email", "events"."device", "events"."report_time" desc);

Environment & setup

No response

@daenash daenash added the bug Something isn't working label Nov 1, 2024
@daenash
Copy link
Author

daenash commented Nov 1, 2024

Quick note: here in the pg-core/view.ts I can't see the casing (or the dialect) being passed down to QueryBuilder()

https://github.com/drizzle-team/drizzle-orm/blob/a21c8e319c9041d728778e67610dd2e769f4b490/drizzle-orm/src/pg-core/view.ts#L51C3-L53C4

if (typeof qb === 'function') {
	qb = qb(new QueryBuilder());
}

daenash added a commit to daenash/drizzle-orm-casing-fix that referenced this issue Nov 1, 2024
@daenash
Copy link
Author

daenash commented Nov 1, 2024

PR: #3387

@L-Mario564 L-Mario564 added drizzle/kit priority Will be worked on next labels Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working drizzle/kit priority Will be worked on next
Projects
None yet
Development

No branches or pull requests

2 participants