最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sql - postgres Error [42P17] ERROR: generation expression is not immutable - Stack Overflow

programmeradmin3浏览0评论
ALTER TABLE my_table
    ADD date date GENERATED ALWAYS AS ((timezone( 'Asia/Kolkata'::text,
       COALESCE( timestamp_with_timezone_1, timestamp_with_timezone_2 ) ))::date) STORED;

I get the following error

[42P17] ERROR: generation expression is not immutable

I have tried using '+5:30' instead of 'Asia/Kolkata' I have tried using at time zone directly with the column, like this:

ALTER TABLE my_table
ADD COLUMN date date GENERATED ALWAYS AS (
  COALESCE(
    (timestamp_with_timezone_1 AT TIME ZONE 'Asia/Kolkata')::date,
    (timestamp_with_timezone_2 AT TIME ZONE 'Asia/Kolkata')::date
  )
) STORED; 

Throws same error.

Can anyone suggest a solution.

ALTER TABLE my_table
    ADD date date GENERATED ALWAYS AS ((timezone( 'Asia/Kolkata'::text,
       COALESCE( timestamp_with_timezone_1, timestamp_with_timezone_2 ) ))::date) STORED;

I get the following error

[42P17] ERROR: generation expression is not immutable

I have tried using '+5:30' instead of 'Asia/Kolkata' I have tried using at time zone directly with the column, like this:

ALTER TABLE my_table
ADD COLUMN date date GENERATED ALWAYS AS (
  COALESCE(
    (timestamp_with_timezone_1 AT TIME ZONE 'Asia/Kolkata')::date,
    (timestamp_with_timezone_2 AT TIME ZONE 'Asia/Kolkata')::date
  )
) STORED; 

Throws same error.

Can anyone suggest a solution.

Share Improve this question edited Mar 17 at 13:21 Shh asked Mar 17 at 13:00 ShhShh 1,01610 silver badges21 bronze badges 3
  • 1 N.B.: I could only reproduce your error when initial columns were without time zone: are you sure timestamp_with_timezone_1 and _2 have one? – Guillaume Outters Commented Mar 17 at 14:51
  • Yes, please clarify what the data types of the columns are. – Laurenz Albe Commented Mar 17 at 15:04
  • Both columns are as stated..timestamp with timezone. – Shh Commented Mar 18 at 3:25
Add a comment  | 

1 Answer 1

Reset to default 0

Do set your time zone after the COALESCE (and it's shorter to write!):

ALTER TABLE my_table
    ADD date date GENERATED ALWAYS AS
    (COALESCE( timestamp_with_timezone_1, timestamp_with_timezone_2 ) AT TIME ZONE 'Asia/Kolkata')
    STORED;

See it in this fiddle.

发布评论

评论列表(0)

  1. 暂无评论