Re: PostgreSQL: Question about rules

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: PostgreSQL: Question about rules
Дата
Msg-id 5785.1163735390@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: PostgreSQL: Question about rules  (Jeff Davis <pgsql@j-davis.com>)
Ответы Re: PostgreSQL: Question about rules
Список 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 по дате отправления:

Предыдущее
От: mike
Дата:
Сообщение: Re: Eliminating bad characters from a database for
Следующее
От: Tom Lane
Дата:
Сообщение: Re: explain analyze taking longer than executing the query?