Uuid always the same with Bun randomUUIDv7() (?)

Hey there I'm working on a sqlite db and have this table: const session = sqliteTable("session", { id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }), ipAddress: text("ip_address").notNull(), userAgent: text("user_agent").notNull(), startedAt: integer("started_at", { mode: "number" }) .default(sql(strftime('%s', 'now'))) .notNull(), endedAt: integer("ended_at", { mode: "number" }), uuid: text("uuid").default(randomUUIDv7()).notNull(), }) When I insert a new session const s = await db.insert(session).values({ ipAddress: ip, userAgent: headers["user-agent"] || "unknown", }).returning({ uuid: session.uuid }) The uuid is always the same. Looking into the sqlite DB I can confirm it's the same. What am I doing wrong?
4 Replies
Tobias
Tobias2mo ago
Try changing it to uuid: text("uuid").default(() => randomUUIDv7()).notNull() I.e. an arrow function within default
_Zaizen_
_Zaizen_OP2mo ago
Gives the error: "SQLite3 can only bind numbers, strings, bigints, buffers, and null" also I should also add this info: If I provide the uuid in the insert everything works fine. const s = await db.insert(session).values({ ipAddress: ip, userAgent: headers["user-agent"] || "unknown", uuid: Bun.randomUUIDv7() }).returning({ uuid: session.uuid }) This insert for example provides a different uuid every time
Tobias
Tobias2mo ago
Try this, this is how I do it in my code and it works, although I don't use the randomUUIDv7() function. uuid: text("uuid").$defaultFn(() => randomUUIDv7()).notNull()
_Zaizen_
_Zaizen_OP2mo ago
seems to work! thanks!

Did you find this page helpful?