[BUG]: (drizzle-orm/sqlite-core) SQLite Schemas do not support nullable/optional fields
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/[email protected]
Describe the Bug
For the following schema.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:
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<unknown>' 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<unknown>' 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<unknown> | Placeholder<string, any>; }): SQLiteInsertBase<SQLiteTableWithColumns<{ name: "projects"; schema: undefined; columns: { id: SQLiteColumn<{ name: "id"; tableName: "projects"; dataType: "number"; ... 11 more ...; generated: undefined; }, {}, {}>; ... 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<unknown> | Placeholder<string, any>; }'.
Overload 2 of 2, '(values: { name: string | SQL<unknown> | Placeholder<string, any>; }[]): SQLiteInsertBase<SQLiteTableWithColumns<{ name: "projects"; schema: undefined; columns: { id: SQLiteColumn<{ name: "id"; tableName: "projects"; dataType: "number"; ... 11 more ...; generated: undefined; }, {}, {}>; ... 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<unknown> | Placeholder<string, any>; }[]'.
*/
}
I note the documentation for Table 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
facing the same issue and it's driving me insane
Based on some [similar] questions I've seen on discord, it appears that this is not exclusive to sqlite, as those people are using postgres.
ETA: it works fine for me using bun-sql and node-postgres. I am not using a typed drizzle instance, simply export const db = drizzle({ client });. Maybe make sure it's not related to that, or your version of drizzle (to rule those out).
My db/index.ts looks like this:
import type { D1Database } from '@cloudflare/workers-types';
import { drizzle } from 'drizzle-orm/d1';
import * as schema from './schema';
export const createDb = (d1: D1Database) => drizzle(d1, { schema });
Versions: [email protected] and @cloudflare/[email protected]
In my testing it seems this has been resolved on the beta branch — bun i drizzle-orm@beta drizzle-kit@beta
Further testing, an edge case bug still exists:
export const projects = table("projects", {
id: integer("id").primaryKey(),
name: text("name").notNull(),
description: text("description").notNull(),
createdAt: text("created_at").notNull().default("CURRENT_TIMESTAMP"),
updatedAt: text("updated_at").notNull().default("CURRENT_TIMESTAMP"),
status: text("status").default("active"), // active, completed, archived
});
const [newProject] = await db
.insert(projects)
.values({
// error: No overload matches this call.
// Overload 1 of 2, '(value: { name: string | SQL<unknown> | Placeholder<string, any>; description: string | SQL<unknown> | Placeholder<string, any>; }): SQLiteInsertBase<SQLiteTableWithColumns<{ name: "projects"; schema: undefined; columns: { ...; }; dialect: "sqlite"; }>, ... 4 more ..., never>', gave the following error.
// Object literal may only specify known properties, and 'status' does not exist in type '{ name: string | SQL<unknown> | Placeholder<string, any>; description: string | SQL<unknown> | Placeholder<string, any>; }'.
// Overload 2 of 2, '(values: { name: string | SQL<unknown> | Placeholder<string, any>; description: string | SQL<unknown> | Placeholder<string, any>; }[]): SQLiteInsertBase<SQLiteTableWithColumns<{ name: "projects"; schema: undefined; columns: { ...; }; 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<unknown> | Placeholder<string, any>; description: string | SQL<unknown> | Placeholder<string, any>; }[]'.
name,
description,
status // commenting out this line resolves the error -- can't insert or update this column at all
})
.returning();
Version: [email protected]
Hey everyone!
I've created this message to send in a batch to all opened issues we have, just because there are a lot of them and I want to update all of you with our current work, why issues are not responded to, and the amount of work that has been done by our team over ~8 months.
I saw a lot of issues with suggestions on how to fix something while we were not responding – so thanks everyone. Also, thanks to everyone patiently waiting for a response from us and continuing to use Drizzle!
We currently have 4 major branches with a lot of work done. Each branch was handled by different devs and teams to make sure we could make all the changes in parallel.
First branch is drizzle-kit rewrite
All of the work can be found on the alternation-engine branch. Here is a PR with the work done: https://github.com/drizzle-team/drizzle-orm/pull/4439
As you can see, it has 167k added lines of code and 67k removed, which means we've completely rewritten the drizzle-kit alternation engine, the way we handle diffs for each dialect, together with expanding our test suite from 600 tests to ~9k test units for all different types of actions you can do with kit. More importantly, we changed the migration folder structure and made commutative migrations, so you won't face complex conflicts on migrations when working in a team.
What's left here:
- We are finishing handling defaults for Postgres, the last being geometry (yes, we fixed the
sridissue here as well). - We are finishing commutative migrations for all dialects.
- We are finishing up the command, so the migration flow will be as simple as
drizzle-kit upfor you.
Where it brings us:
- We are getting drizzle-kit into a new good shape where we can call it
[email protected]!
Timeline:
- We need ~2 weeks to finish all of the above and send this branch to beta for testing.
Second big branch is a complex one with several HUGE updates
- Bringing Relational Queries v2 finally live. We've done a lot of work here to actually make it faster than RQBv1 and much better from a DX point of view. But in implementing it, we had to make another big rewrite, so we completely rewrote the drizzle-orm type system, which made it much simpler and improved type performance by ~21.4x:
(types instantiations for 3300 lines production drizzle schema + 990 lines relations)
TS v5.8.3: 728.8k -> 34.1k
TS v5.9.2: 553.7k -> 25.4k
You can read more about it here.
What's left here:
- We have 1 issue with TS that is already in progress of being fixed. The issue and Post about fixing.
Where it brings us:
- We are getting drizzle-orm into a new good shape where we can call it
[email protected]!
Breaking changes:
- We will have them, but we will have open channels for everyone building on top of drizzle types, so we can guide you through all the changes.
Third branch is adding support for CockroachDB and MSSQL dialects
Support for them is already in the alternation-engine branch and will be available together with the drizzle-kit rewrite.
Summary
All of the work we are doing is crucial and should be done sooner rather than later. We've received a lot of feedback and worked really hard to find the best strategies and decisions for API, DX, architecture, etc., so we can confidently mark it as v1 and be sure we can improve it and remain flexible for all the features you are asking for, while becoming even better for everyone building on top of the drizzle API as well.
We didn't want to stay with some legacy decisions and solutions we had, and instead wanted to shape Drizzle in a way that will be best looking ahead to 2025–2026 trends (v1 will get proper effect support, etc.).
We believe that all of the effort we've put in will boost Drizzle and benefit everyone using it.
Thanks everyone, as we said, we are here to stay for a long time to build a great tool together!
Timelines
We are hoping to get v1 for drizzle in beta this fall and same timeline for latest. Right after that we can go through all of the issues and PRs and resond everyone. v1 for drizzle should close ~70% of all the bug tickets we have, so on beta release we will start marking them as closed!
Hi @grrowl. Could you share your schema - the one you're passing during the DB instance creation?
export const createDb = (d1: D1Database) => drizzle(d1, { schema });