drizzle-team / drizzle-team/drizzle-orm

[BUG]: Invalid SQL generated for unique indexes, missing newline before statement-breakpoint

Open
#4,814 1 comment 7 reactions 0 assignees View on GitHub
bug bug/fixed-in-beta
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Report hasn't been filed before.

- [x] 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.3

### What version of `drizzle-kit` are you using?

0.31.4

### Other packages

expo-sqlite@14.1.0

### Describe the Bug

**What is the undesired behavior?**
drizzle-kit generates invalid SQL migrations when using `.unique()` constraints on SQLite tables. The generated `CREATE UNIQUE INDEX` statements are missing newlines before `--> statement-breakpoint` comments, causing SQL syntax errors during migration execution.

Error when executing:
```
Error: Call to function 'NativeDatabase.execSync' has been rejected.
→ Caused by: near "(": syntax error
```

This breaks database initialization for SQLite projects using `.unique()`, my current workaround is manually editing the migration file to add the new line

**What are the steps to reproduce it?**

1. Create a schema with unique constraint:
```typescript
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";

export const testTable = sqliteTable("test_table", {
id: integer("id").primaryKey(),
uniqueField: text("uniqueField").unique(),
});
```

2. Create drizzle config:
```typescript
import type { Config } from "drizzle-kit";
export default {
schema: "./schema.ts",
out: "./drizzle",
dialect: "sqlite",
driver: "expo",
} satisfies Config;
```

3. Generate migration with `npx drizzle-kit generate`

4. Observe the generated SQL contains syntax error

**What is the desired result?**
Valid SQL migration that executes without syntax errors:
```sql
CREATE TABLE `test_table` (
`id` integer PRIMARY KEY NOT NULL,
`uniqueField` text
);
--> statement-breakpoint
CREATE UNIQUE INDEX `test_table_uniqueField_unique` ON `test_table` (`uniqueField`);
--> statement-breakpoint
```

**What database engine are you using?**
SQLite with expo-sqlite driver

**Do you think this bug pertains to a specific database driver?**
This affects SQLite specifically. The bug is in the SQL generation logic for SQLite unique indexes.

**Runtime**
Discovered whilst using React Native with Expo (using expo-sqlite)

Logcat screenshot:
Image

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.