drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: SQLite `.primaryKey().notNull()`

Open
#2,611 5 comments 1 reaction 0 assignees View on GitHub
db/sqlite improvement priority
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

Drizzle Kit converts `.primaryKey()` to `PRIMARY KEY NOT NULL`.

```js
export const userTable = sqliteTable('user', {
id: integer('id').primaryKey(),
```

```sql
CREATE TABLE `user` (
`id` integer PRIMARY KEY NOT NULL,
```

This is not necessary. `integer PRIMARY KEY` must be non-NULL.

> The value of an INTEGER PRIMARY KEY column must always be a non-NULL integer because the INTEGER PRIMARY KEY is an alias for the ROWID. If you try to insert a NULL into an INTEGER PRIMARY KEY column, SQLite automatically convert the NULL into a unique integer.
>
> https://www.sqlite.org/quirks.html

---

However, there are cases where explicit `.notNull()` is necessary.

For example, in a one-to-one relationship between two tables:

```js
export const userTable = sqliteTable('user', {
id: integer('id').primaryKey(),
});

export const profileTable = sqliteTable('profile', {
userId: integer('user_id')
.primaryKey()
.notNull() // This has no effect.
.references(() => userTable.id),
});
```

Since `user_id` is a primary-key that references another table, a value must be provided on insertion.

However, this cannot be enforced currently.

```js
// This does not cause a TypeScript error.
await db.insert(profileTable).values({});
```

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.