© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
2 replies
Drew

Code generated value

I'm using SQLite. How can I create a custom type with an auto-generated value which is generated in code and not the database? I managed to get it to work, but I have to pass a blank value for the field whenever creating records otherwise I get a type error, and I'm having trouble finding the right combination of options.

import {customType, sqliteTable } from "drizzle-orm/sqlite-core";
import { createId } from "@paralleldrive/cuid2";

const cuid2 = customType<{ data: string; notNull: true }>({
  dataType() { return "text"; },
  toDriver(): string { return createId(); },
});

const users = sqliteTable("users",
  { id: cuid2("id").primaryKey() }
);

db.insert(users).values({id: ""}).run();
import {customType, sqliteTable } from "drizzle-orm/sqlite-core";
import { createId } from "@paralleldrive/cuid2";

const cuid2 = customType<{ data: string; notNull: true }>({
  dataType() { return "text"; },
  toDriver(): string { return createId(); },
});

const users = sqliteTable("users",
  { id: cuid2("id").primaryKey() }
);

db.insert(users).values({id: ""}).run();


If
{id: ""}
{id: ""}
is left out of
values
values
, TypeScript complains: "property
id
id
is missing. On the other hand, setting a default or using
default: true
default: true
as configuration resolves the TypeScript error, but inserting records fails with a runtime error: UNIQUE constraint failed: users.id.

Is there a better way that allows omitting the field on insert, does not result in a type error, and safely and reliably generates an value?
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Generated Collate value
Drizzle TeamDTDrizzle Team / help
3y ago
Foreign Key Reference to auth Schema Not Generated in Code
Drizzle TeamDTDrizzle Team / help
3y ago
rpc error: code = InvalidArgument desc = Invalid default value
Drizzle TeamDTDrizzle Team / help
3y ago
Generated Column Unique Constraint
Drizzle TeamDTDrizzle Team / help
10mo ago