drizzle-team / drizzle-team/drizzle-orm

[BUG]: (drizzle-orm/sqlite-core) SQLite Schemas do not support nullable/optional fields

Open
#4,219 7 comments 7 reactions 0 assignees View on GitHub
bug db/sqlite priority qb/crud
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.40.0

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

0.30.5

### Other packages

@cloudflare/workers-types@4.20250303.0

### Describe the Bug

For the following schema.ts

```ts
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';

export const projects = sqliteTable('projects', {
id: integer('id').primaryKey(),
name: text('name').notNull(), // non-nullable, required
description: text('description'), // nullable, optional
createdAt: text('created_at').notNull().default('CURRENT_TIMESTAMP'), // non-nullable, but optional due to default
status: text('status').notNull().default('active'), // non-nullable, but optional due to default
});
```

Referencing any field that is optional is a type error, whether select or insert:

```ts
import { createDb } from 'db';
import {
projects
} from 'db/schema';
import { desc } from 'drizzle-orm';

export async function typeTest() {
const db = createDb();

const _select = await db
.select()
.from(projects)
/* ^?: const _select: {
id: number;
name: string;
description: string;
createdAt: string;
updatedAt: string;
status: string;
}[] */

const _selectOrdered = await db
.select()
.from(projects)
.orderBy(desc(projects.createdAt));
/* ^?: ts: No overload matches this call.
Overload 1 of 2, '(builder: (aliases: { id: SQLiteColumn<{ name: "id"; tableName: "projects"; dataType: "number"; columnType: "SQLiteInteger"; data: number; driverParam: number; notNull: true; hasDefault: true; isPrimaryKey: true; isAutoincrement: false; ... 4 more ...; generated: undefined; }, {}, {}>; ... 4 more ...; status: SQLiteColumn<...>; }) => ValueOrArray<...>): Omit<...>', gave the following error.
Argument of type 'SQL' is not assignable to parameter of type '(aliases: { id: SQLiteColumn<{ name: "id"; tableName: "projects"; dataType: "number"; columnType: "SQLiteInteger"; data: number; driverParam: number; notNull: true; hasDefault: true; isPrimaryKey: true; isAutoincrement: false; ... 4 more ...; generated: undefined; }, {}, {}>; ... 4 more ...; status: SQLiteColumn<......'.
Type 'SQL' provides no match for the signature '(aliases: { id: SQLiteColumn<{ name: "id"; tableName: "projects"; dataType: "number"; columnType: "SQLiteInteger"; data: number; driverParam: number; notNull: true; hasDefault: true; isPrimaryKey: true; isAutoincrement: false; ... 4 more ...; generated: undefined; }, {}, {}>; ... 4 more ...; status: SQLiteColumn<...>; }): ValueOrArray<...>'.
*/

const _insert = await db
.insert(projects)
.values({
name: 'Test Project',
}).returning()
/* ^?: const _insert: {
id: number;
name: string;
description: string;
createdAt: string;
updatedAt: string;
status: string;
}[]
*/

const _insertd = await db
.insert(projects)
.values({
name: 'Test Project',
description: 'This is a test project',
});
/* ^?: ts: No overload matches this call.
Overload 1 of 2, '(value: { name: string | SQL | Placeholder; }): SQLiteInsertBase; ... 4 more ...; status: SQLiteColumn<...>; }; dialect: "sqlite"; }>, ... 4 more ..., never>', gave the following error.
Object literal may only specify known properties, and 'description' does not exist in type '{ name: string | SQL | Placeholder; }'.
Overload 2 of 2, '(values: { name: string | SQL | Placeholder; }[]): SQLiteInsertBase; ... 4 more ...; status: SQLiteColumn<...>; }; dialect: "sqlite"; }>, ... 4 more ..., never>', gave the following error.
Object literal may only specify known properties, and 'name' does not exist in type '{ name: string | SQL | Placeholder; }[]'.
*/
}
```

I note [the documentation for Table Schemas](https://orm.drizzle.team/docs/schemas) under SQLite reads "SQLite does not have support for schemas 😕"; it seems it's just out of date [as `examples/cloudflare-d1` exists in the repo which describes how to use `sqlite-core`](https://github.com/drizzle-team/drizzle-orm/blob/main/examples/cloudflare-d1/src/schema.ts)

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.