drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.69k stars 493 forks source link

[BUG]: SQLite/Turso libsql: Drizzle (Studio) not running functions when adding records #2198

Open Pramuspl opened 2 months ago

Pramuspl commented 2 months ago

What version of drizzle-orm are you using?

0.30.9

What version of drizzle-kit are you using?

0.20.17

Describe the Bug

For a following table:

export const users = sqliteTable(
  "organizations",
  {
    id: text("id")
      .primaryKey()
      .default(sql`(uuid4())`),
    email: text("email").notNull(),
    updatedAt: text("updated_at")
      .notNull()
      .default(sql`current_timestamp`),
);
CREATE TABLE `users` (
    `id` text PRIMARY KEY DEFAULT (uuid4()) NOT NULL,
    `email` text NOT NULL,
    `updated_at` text DEFAULT current_timestamp NOT NULL
);

When I add a new record via Drizzle Studio, I noticed that the SQLite functions are not being fired. They work fine when adding records manually using SQL queries but not when I use Drizzle Runner or via "Add record" button. Interestingly, the behaviour is slightly different in both cases. I presume the same error will occur when executing the queries from code.

Zrzut ekranu 2024-04-23 o 20 07 44

Expected behavior

Execute uuid4() and current_timestamp on row creation regardless of the method

Environment & setup

No response

miguelrk commented 1 month ago

This is happening to me as well. Doing e.g. createdAt: text("created_at").notNull().default(sql(current_timestamp)) will simply write (current_timestamp) to the database when editing in the studio. I presume this would also be the case when doing it programatically. What works, is using $default/$defaultFn instead like so: createdAt: text("created_at").notNull().$default(() => new Date().toISOString()), although this would only work at runtime, and not at the database level.