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
23.4k stars 573 forks source link

[BUG]: Issue with quoted default string values #2122

Open realmikesolo opened 5 months ago

realmikesolo commented 5 months ago

What version of drizzle-orm are you using?

0.30.7

What version of drizzle-kit are you using?

0.20.14

Describe the Bug

Drizzle kit incorrectly handles string values with quotes for default statement which leads to syntax errors in PostgreSQL, MySQL, and SQLite.

For this schema:

export const tests = pgTable('table', {
  id: serial('id').primaryKey(),
  column: text('name').notNull().default("Column's value"), // default value with quote
});

generates this SQL:

CREATE TABLE IF NOT EXISTS "table" (
        "id" serial PRIMARY KEY NOT NULL,
        "name" text DEFAULT 'Column's value' NOT NULL
);

which leads to this error:

error: syntax error at or near "s"

Expected behavior

The expectation is for the Drizzle kit to generate correct SQL without syntax errors for all supported dialects. Currently, there is a workaround for PostgreSQL and MySQL:

export const tests = pgTable('table', {
  id: serial('id').primaryKey(),
  column: text('name').notNull().default("Column''s value"), // '' instead of '
});

The generated SQL will correctly interpret the two single quotes as a single quote in the application

CREATE TABLE IF NOT EXISTS "table" (
        "id" serial PRIMARY KEY NOT NULL,
        "name" text DEFAULT 'Column''s value' NOT NULL
);

Environment & setup

No response

Louisna123 commented 5 months ago

I got the same issue. The shema generated from SQLite should add quotes to Default value, while it didn't. "drizzle-orm": "^0.30.8", "drizzle-kit": "^0.20.14",