Drizzle TeamDT
Drizzle Team3y ago
3 replies
LaunchThat.App

[How To?] Generate uuids properly on cloudflare d1 sqlite...

I am running an app with Drizzle, TRPC, and Cloudflare D1 sqlite database:

My initial project databasse schema looks like:
export const CarTable = sqliteTable('Car', {
  id: text('id').primaryKey(),
  make: text('make').notNull(),
  model: text('model').notNull(),
  year: integer('year').notNull(),
  color: text('color').notNull(),
  price: real('price').notNull(),
  mileage: integer('mileage').notNull(),
  fuelType: text('fuelType').notNull(),
  transmission: text('transmission').notNull(),
})


I am trying to add a uuid column to the table in addition to autoincrementing the id column and making the other columns not required. First I am trying to ad a uuid column.

I have researched this in a couple places, cant seem to find what I am looking for. I want a uuid column that automatically generates a uuid when i insert a record. Seems to be possible with mysql but not sqlite?

Here is the uuid column I have been able to push through drizzle to the database:
uuid: binary('uuid', { length: 16 }).default(`hex(randomblob(16))`), // Generate a random 16-byte binary string

I have been able to generate the migration (
drizzle-kit generate:sqlite --schema=./src/db/schema.ts --out=./migrations
)
This gets pushed to the database via wrangler with a
wrangler d1 migrations apply production --local
command.
Was this page helpful?