drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Warn users when adding a `NOT NULL` column to an existing table

Open
#4,168 0 comments 2 reactions 0 assignees View on GitHub
db/sqlite drizzle/kit enhancement improvement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Feature hasn't been suggested before.

- [x] I have verified this feature I'm about to request hasn't been suggested before.

### Describe the enhancement you want to request

Adding a `.notNull()` column to an existing table results in a generic SQLite error.

```sql
/* Generated by drizzle-kit */
ALTER TABLE `program` ADD `is_pinned` integer NOT NULL;
```

> [⣷] applying migrations...
> DrizzleError: Failed to run the query 'ALTER TABLE `program` ADD `is_pinned` integer NOT NULL;

```
code: 'SQLITE_ERROR'
```

This can be avoided by adding a DEFAULT value for that column, but this is still a workaround:

```sql
ALTER TABLE `program` ADD `is_pinned` integer NOT NULL DEFAULT 0;
/* No longer matches the schema */
```

This could be fixed by:

- running `drizzle-kit generate` asks for the default value for the new column
- generate `ALTER TABLE` statement that first applies `DEFAULT` and then removes it

The above method requires recreating the table in SQLite.

Providing a comment and a possible reference material could be helpful.

```sql
/* This will fail if there are existing rows in the table */
ALTER TABLE `program` ADD `is_pinned` integer NOT NULL;
```

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.