drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.52k stars 487 forks source link

[BUG]: Postgres push fails due to lack of quotes #2396

Open jasongerbes opened 1 month ago

jasongerbes commented 1 month ago

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

0.21.4

Describe the Bug

The drizzle-kit push command fails when using a Postgres database if a table has a composite primary key and it's name includes a "-".

This issue appears to be due to the CONSTRAINT name not being wrapped with quotes:

> drizzle-kit push

drizzle-kit: v0.21.4
drizzle-orm: v0.30.10

No config path provided, using default path
Reading config file '/Users/jasongerbes/Documents/GitHub/test-app/drizzle.config.ts'
Using '@vercel/postgres' driver for database querying
[Warning] '@vercel/postgres' can only connect to remote Neon/Vercel Postgres/Supabase instances through a websocket
[✓] Pulling schema from database...
 Warning  You are about to execute current statements:

ALTER TABLE "example-table" DROP CONSTRAINT example-table_userId_credentialID_pk;
--> statement-breakpoint
ALTER TABLE "example-table" ADD CONSTRAINT example-table_userId_credentialID_pk PRIMARY KEY(userId,credentialID);

error: syntax error at or near "-"

Expected behavior

The drizzle-kit push command should wrap CONSTRAINT names in quotes to prevent issues due to the inclusion of "-" in a table's name.

Environment & setup

Example table schema:

import { pgTable, primaryKey, text } from 'drizzle-orm/pg-core'

export const examples = pgTable(
  'example-table',
  {
    credentialID: text('credentialID').notNull().unique(),
    userId: text('userId').notNull(),
  },
  (example) => ({
    compositePK: primaryKey({
      columns: [example.userId, example.credentialID],
    }),
  }),
)

Reproduction steps:

  1. Set up a Postgres database (e.g. Vercel Postgres)
  2. Run drizzle-kit push
  3. Run drizzle-kit push again