Re: PostgreSQL: Question about rules - Mailing list pgsql-general

From Tom Lane
Subject Re: PostgreSQL: Question about rules
Date
Msg-id 5785.1163735390@sss.pgh.pa.us
Whole thread Raw
In response to Re: PostgreSQL: Question about rules  (Jeff Davis <pgsql@j-davis.com>)
Responses Re: PostgreSQL: Question about rules
List pgsql-general
Jeff Davis <pgsql@j-davis.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

pgsql-general by date:

Previous
From: mike
Date:
Subject: Re: Eliminating bad characters from a database for
Next
From: Tom Lane
Date:
Subject: Re: explain analyze taking longer than executing the query?