drizzle-team / drizzle-team/drizzle-orm

[BUG]: Sqlite Migration broken when adding column

Open
#5,822 0 comments 0 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.45.2

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

0.31.10

### Other packages

_No response_

### Describe the Bug

If you change a column type (which means the table schema has to recreated entirely) and add new columns the migrations are broken.

the migration select all values from the old table and insert them into the new table. but it select columns from the old table that do not exist yet

Schema
```ts
export const test = sqliteTable('test', {
column1: text('column1').notNull(),
column2: text('column2'),
});
```

Migration
```sql
CREATE TABLE `test` (
`column1` text NOT NULL,
`column2` text NOT NULL
);
```

Schema
```ts
export const test = sqliteTable('test', {
column1: text('column1').notNull(),
column2: text('column2'),
column3: text('column3'),
});
```

Migration
```sql
PRAGMA foreign_keys=OFF;--> statement-breakpoint
CREATE TABLE `__new_test` (
`column1` text NOT NULL,
`column2` text,
`column3` text
);
--> statement-breakpoint
-- this insert statement is broken, can't select column3 from test, as this does not exist
INSERT INTO `__new_test`("column1", "column2", "column3") SELECT "column1", "column2", "column3" FROM `test`;--> statement-breakpoint
DROP TABLE `test`;--> statement-breakpoint
ALTER TABLE `__new_test` RENAME TO `test`;--> statement-breakpoint
PRAGMA foreign_keys=ON;
```

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.