Skip to content
Advertisement

PostgreSQL – Cast generated column type

I tried to create generated column from other JSON type column with ALTER TABLE "Invoice" ADD COLUMN created Integer GENERATED ALWAYS AS (data ->> 'created') STORED;

When I execute this I get error ERROR: column "created" is of type integer but default expression is of type text HINT: You will need to rewrite or cast the expression. SQL state: 42804

I tried to cast it with CAST function and :: operator but with no lack. Is there any way to do it? Or maybe I should generate this column differently?

Thanks

Advertisement

Answer

How about converting the value to an int?

ALTER TABLE "Invoice" ADD COLUMN created Integer
    GENERATED ALWAYS AS ( (data ->> 'created')::int ) STORED;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement