column default dependant on another columns value

From: "Fernando Hevia" <fhevia(at)ip-tel(dot)com(dot)ar>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: column default dependant on another columns value
Date: 2008-07-01 20:12:30
Message-ID: 001e01c8dbb6$ca24f690$8f01010a@iptel.com.ar
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Broersma 2008-07-01 20:44:46 Re: column default dependant on another columns value
Previous Message Richard Huxton 2008-07-01 15:17:30 Re: Need a sample Postgre SQL script