Re: PostgreSQL: Question about rules

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jeff Davis <pgsql(at)j-davis(dot)com>
Cc: Jeremy Smith <postgres(at)duckwizard(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: PostgreSQL: Question about rules
Date: 2006-11-17 03:49:50
Message-ID: 5785.1163735390@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jeff Davis <pgsql(at)j-davis(dot)com> writes:
> Interesting question. It seems like you're trying to make a default
> value for a view that's based on the underlying table's default value.

> I think the normal way to do this is to _not_ have a default value on
> the underlying table, and instead use the rule to define the default
> value. Does that work for you?

Actually, the best way to do that is to attach a default to the view
itself.

CREATE VIEW v AS SELECT ... ;
CREATE RULE ... for insert on v ... ;
ALTER TABLE v ALTER COLUMN c DEFAULT whatever;

In this formulation the rule is not responsible for substituting any
default values, it just does what it's told. This is better than the
COALESCE approach because the latter does the wrong thing if one is
explicitly inserting NULL.

I'm not sure this answers the OP's problem though, as it sounded like he
wanted a default dependent on other inserted values, which is something
you can't do with either a table or a view.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2006-11-17 04:01:22 Re: explain analyze taking longer than executing the query?
Previous Message mike 2006-11-17 03:49:11 Re: Eliminating bad characters from a database for