column default dependant on another columns value - Mailing list pgsql-sql

From Fernando Hevia
Subject column default dependant on another columns value
Date
Msg-id 001e01c8dbb6$ca24f690$8f01010a@iptel.com.ar
Whole thread Raw
Responses Re: column default dependant on another columns value  ("Richard Broersma" <richard.broersma@gmail.com>)
List pgsql-sql
Hi list,
Given a table with columns seconds and minutes, how can I have minutes be
computed automatically at the insert statement?
I tried:
ALTER TABLE table1 ALTER COLUMN minutes SET default (seconds/60);
Postgres' answer was:
ERROR:  cannot use column references in default expression
So I gave rules a look but it seems rules apply to the entire row.
CREATE RULE "my_rule" AS ON 
INSERT TO table1
WHERE minutes is null
DO INSTEAD 
INSERT INTO table1 (column1, column2, seconds, minutes) 
VALUES(new.column1, new.column2, new.seconds, new.seconds/60);
Is this correct? Is there another (better/simpler) way to achieve this?
Regards,
Fernando



pgsql-sql by date:

Previous
From: Richard Huxton
Date:
Subject: Re: Need a sample Postgre SQL script
Next
From: "Richard Broersma"
Date:
Subject: Re: column default dependant on another columns value