Reputation: 41
I'm using graphql, typescript, mikro-orm and postgresql to implement crud on posts. The property createdAt is having issues:
DriverException: alter table "post" alter column "created_at" type timestamptz(0) using ("created_at"::timestamptz(0)); - cannot cast type jsonb to timestamp with time zone
This is the entity of the createdAt property:
@Field(() => String)
@Property({ type: "date" })
createdAt = new Date();
And these two are the migrations generated from the createdAt entity
this.addSql('alter table "post" drop constraint if exists "post_created_at_check";');
this.addSql('alter table "post" alter column "created_at" type timestamptz(0) using ("created_at"::timestamptz(0));');
How would I go by fixing this error? Which types do I need to change?
Upvotes: 2
Views: 3215
Reputation: 41
Alright, I solved my problem. Before that, I'd like to thank the creator of Mikro-orm for responding.
I decided to drop the whole database and make a new one.
It still wasn't working and I found out the yarn tsc -w
had a few bugs, which meant I had to delete the dist
folder and run the command again.
I finally started to recieve different errors. I first thought to replace type timestamptz(0)
with type String
but apparently I don't know my types properly.
I searched around and finally changed the types:
this.addSql(`CREATE TABLE "post" ("id" SERIAL NOT NULL, "title" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now())`);
Upvotes: 2
Reputation: 18389
The column already exists and is defined as jsonb
. Postgres won't allow you to convert it to datetime, you will need to drop it first.
So adjust your generated migration file to first drop the column and re-add it again, or if you don't know how to construct the queries, you can remove this migration, comment out the property, generate new one that will drop it for you, execute it and then uncomment and generate new one that will add it correctly.
Upvotes: 1