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
23.02k stars 553 forks source link

[BUG]: planetscale - now() and current_timestamp() doesn't work when FSP is specified for timestamp #472

Open Itisfilipe opened 1 year ago

Itisfilipe commented 1 year ago

What version of drizzle-orm are you using?

0.23.13

What version of drizzle-kit are you using?

0.17.4

Describe the Bug

Create table fails on planetscale if FSP not specified for now() and current_timestamp

CREATE TABLE `test_table` (
    `created_at` timestamp(2) NOT NULL DEFAULT (now())
);

leads to

ERROR 1067 (42000): target: ***.-.primary: vttablet: rpc error: code = InvalidArgument desc = Invalid default value for 'created_at' (errno 1067) (sqlstate 42000) (CallerID: planetscale-admin): Sql: "create table test_table (\n\tcreated_at timestamp(2) not null default now()\n)", BindVars: {REDACTED}

but it works if the FSP is added

CREATE TABLE `test_table` (
    `created_at` timestamp(2) NOT NULL DEFAULT (now(2))
);

the same is valid for current_timestamp

CREATE TABLE `test_table2` (
    `updated_at` timestamp(2) NOT NULL DEFAULT (now(2)) ON UPDATE CURRENT_TIMESTAMP
);
ERROR 1294 (HY000): target: ***.-.primary: vttablet: rpc error: code = InvalidArgument desc = Invalid ON UPDATE clause for 'updated_at' column (errno 1294) (sqlstate HY000) (CallerID: planetscale-admin): Sql: "create table test_table2 (\n\tupdated_at timestamp(2) not null default now(2) on update current_timestamp()\n)", BindVars: {REDACTED}

it works for

CREATE TABLE `test_table2` (
    `updated_at` timestamp(2) NOT NULL DEFAULT (now(2)) ON UPDATE CURRENT_TIMESTAMP(2)
);

Expected behavior

No response

Environment & setup

No response

AndriiSherman commented 1 year ago

Thanks! Will add this fix. Currently for defaults you can use

.default(sql`(now(2))`)
.default(sql`CURRENT_TIMESTAMP(2)`)

For onUpdate no workarounds for now. Only by manually changing sql file before applying it to database

steviec commented 1 year ago

I have this same issue. I can manually change the sql, but it's blocking me from using my planetscale push:mysql workflow :(.