Skip to content

[SQLite-kit] Wrap expression defaults in parentheses#5652

Open
aayushbaluni wants to merge 1 commit intodrizzle-team:mainfrom
aayushbaluni:fix/5634-sqlite-default-parens
Open

[SQLite-kit] Wrap expression defaults in parentheses#5652
aayushbaluni wants to merge 1 commit intodrizzle-team:mainfrom
aayushbaluni:fix/5634-sqlite-default-parens

Conversation

@aayushbaluni
Copy link
Copy Markdown

What's Changed

Fixes #5634

When drizzle-kit push recreates a SQLite table (e.g. to add a named UNIQUE constraint), expression defaults like datetime('now') are emitted without the parentheses SQLite requires:

-- Before (broken):
`created_at` text DEFAULT datetime('now')  -- SQLITE_ERROR: near "(": syntax error

-- After (fixed):
`created_at` text DEFAULT (datetime('now'))  -- valid SQLite

Root Cause

SQLiteCreateTableConvertor and SQLiteAlterTableAddColumnConvertor in sqlgenerator.ts interpolate column.default directly into the SQL without wrapping expression defaults. SQLite's grammar requires non-literal defaults (function calls, expressions) to be enclosed in parentheses.

Fix

Added wrapSqliteExprDefault() — a small helper that detects whether a default value is a simple literal (string, number, NULL, CURRENT_TIMESTAMP, etc.) or an expression, and wraps expressions in parentheses. Applied to both SQLiteCreateTableConvertor.convert() and SQLiteAlterTableAddColumnConvertor.convert().

Literal defaults (strings, numbers, booleans, CURRENT_TIMESTAMP) pass through unchanged — no double-wrapping.

Tests Added

Three new test cases in sqlite-columns.test.ts:

  1. Expression defaults wrapped in parentheses during CREATE TABLE
  2. Expression defaults wrapped in parentheses during ALTER TABLE ADD COLUMN
  3. Literal defaults NOT double-wrapped (regression guard)

All existing SQLite tests (columns, tables, generated, checks, libsql) continue to pass.

Made with Cursor

SQLite requires expression defaults (e.g. datetime('now')) to be
enclosed in parentheses. Without this, drizzle-kit push generates
invalid SQL like DEFAULT datetime('now') instead of
DEFAULT (datetime('now')), causing SQLITE_ERROR syntax errors during
table recreation.

Fixes drizzle-team#5634

Made-with: Cursor
@aayushbaluni aayushbaluni force-pushed the fix/5634-sqlite-default-parens branch from 5f26da1 to 2807309 Compare May 2, 2026 05:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: SQLite push generates DEFAULT datetime('now') without parentheses, causing syntax error

1 participant