2

I created this table in Planetscale

CREATE TABLE `community` (
    `id` serial PRIMARY KEY,
    `name` varchar(256),
    `email` varchar(256) NOT NULL,
    `created_at` timestamp(2) NOT NULL DEFAULT (now()),
    `last_updated_at` timestamp(2)
);

But if I try to create the this unique index

CREATE UNIQUE INDEX email_unique_index ON community (`email`);

I get this error:

18:01:21    CREATE UNIQUE INDEX email_unique_index ON community (`email`)   Error Code: 1067. target: gili.-.primary: vttablet: rpc error: code = InvalidArgument desc = Invalid default value for 'created_at' (errno 1067) (sqlstate 42000) (CallerID: ewuobpfp487ve5g2p79l): Sql: "alter table community add UNIQUE INDEX email_unique_index (email)", BindVars: {REDACTED}  0.418 sec

I could create the same table and index in my local mysql instance. I was able to create the index in planetscale after i removed DEFAULT (now()) as show below.

CREATE TABLE `community` (
    `id` serial PRIMARY KEY,
    `name` varchar(256),
    `email` varchar(256) NOT NULL,
    `created_at` timestamp(2) NOT NULL,
    `last_updated_at` timestamp(2)
);
CREATE UNIQUE INDEX email_unique_index ON community (`email`);

Why does this happen in Planetscale? Is there a fix for it?

1
  • If you can afford to lose the precision of the created_at column you can change it to: `created_at` timestamp NOT NULL DEFAULT now() and the index email_unique_index should be created without problem. Otherwise you can directly ask the PlanetScale :: Discussions.
    – wchiquito
    Mar 16 at 22:06

1 Answer 1

2

I had the exact same problem! You need to remove the NOT NULL constraint. That's the issue. Like this: created_at timestamp(2) DEFAULT (now()). That did it for me.

Update: This is the corrected SQL without having to remove the NOT NULL constraint. The problematic part was (now()), which I changed to now(2). I tested this in my Planetscale instance, and it worked.

CREATE TABLE `community` (
    `id` serial PRIMARY KEY,
    `name` varchar(256),
    `email` varchar(256) NOT NULL,
    `created_at` timestamp(2) NOT NULL DEFAULT now(2),
    `last_updated_at` timestamp(2)
);

And this is how you do it with Drizzle ORM:

{
  createdAt: timestamp("created_at", { fsp: 2 })
    .notNull()
    .default(sql`now(2)`),
}
6
  • Thanks Carlos! But removing NOT NULL makes created_at optional in TypeScript, when in fact it isn't. I want to avoid that
    – inginia
    Sep 14 at 14:51
  • @inginia I'm having the exact same issue, did you solve this?
    – Acework
    Sep 15 at 23:06
  • @inginia. What are you using for the types? Prisma? Sep 19 at 13:37
  • @CarlosGalarza I use drizzle orm orm.drizzle.team
    – inginia
    Sep 19 at 18:51
  • @Acework not yet :( I just set it manually
    – inginia
    Sep 19 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.