drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

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

Open Pramuspl opened this issue 1 year ago • 1 comments

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

Pramuspl avatar Apr 23 '24 18:04 Pramuspl

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.

miguelrk avatar May 14 '24 11:05 miguelrk

@Pramuspl @miguelrk Hey! This should be fixed, try it.

RomanNabukhotnyi avatar Aug 26 '24 10:08 RomanNabukhotnyi