Обсуждение: Feature request

Поиск
Список
Период
Сортировка

Feature request

От
Дата:
So why I can do:
insert into (id) values ( default )=20
and can not do in my trigger:
new.id =3D default

Why?=

Re: Feature request

От
"Pavel Stehule"
Дата:
Hello

2008/5/5  <Eugen.Konkov@aldec.com>:
> So why I can do:
> insert into (id) values ( default )
> and can not do in my trigger:
> new.id = default
> Why?

it's not possible - because you cannot mix PL/pgSQL statement
(assignment) and SQL stetement.

syntax of assignment
:
variable := expression

expression is independent on variable, so there are not any "default"
expression. PL/pgSQL and SQL are independent environments. If you need
default values, simply don't change field.

Regards
Pavel Stehule

Re: Feature request

От
Дата:
There is situation where I check values and if they do not satisfy some
condition I need to set default.
What is the best way to do that?

----- Original Message -----
From: "Pavel Stehule" <pavel.stehule@gmail.com>
To: <Eugen.Konkov@aldec.com>
Cc: <pgsql-bugs@postgresql.org>
Sent: Monday, May 05, 2008 8:10 PM
Subject: Re: [BUGS] Feature request


> Hello
>
> 2008/5/5  <Eugen.Konkov@aldec.com>:
>> So why I can do:
>> insert into (id) values ( default )
>> and can not do in my trigger:
>> new.id = default
>> Why?
>
> it's not possible - because you cannot mix PL/pgSQL statement
> (assignment) and SQL stetement.
>
> syntax of assignment
> :
> variable := expression
>
> expression is independent on variable, so there are not any "default"
> expression. PL/pgSQL and SQL are independent environments. If you need
> default values, simply don't change field.
>
> Regards
> Pavel Stehule

Re: Feature request

От
"Pavel Stehule"
Дата:
2008/5/5  <Eugen.Konkov@aldec.com>:
> There is situation where I check values and if they do not satisfy some
> condition I need to set default.
> What is the best way to do that?

don't do it :)

One advice for triggers - do only check of values and newer fix or
correct values. It's general advice. I like triggers, but triggers
have to be clean. Too much smart triggers uncleans bussiness logic.
Try to move some logic from trigger to some function (stored
function).

It's not easy get default values in body of trigger, because any DML
statetement can start trigger recursion. I don't know clean solution.
Maybe use function for default value, if it's possible.

create or replace function default_generator() ...

create table foo(a integer default default_generator())

and then:

new.id = default_generator();

it's probably one clean solution

Pavel

>
> ----- Original Message ----- From: "Pavel Stehule" <pavel.stehule@gmail.com>
> To: <Eugen.Konkov@aldec.com>
> Cc: <pgsql-bugs@postgresql.org>
> Sent: Monday, May 05, 2008 8:10 PM
> Subject: Re: [BUGS] Feature request
>
>
>> Hello
>>
>> 2008/5/5  <Eugen.Konkov@aldec.com>:
>>>
>>> So why I can do:
>>> insert into (id) values ( default )
>>> and can not do in my trigger:
>>> new.id = default
>>> Why?
>>
>> it's not possible - because you cannot mix PL/pgSQL statement
>> (assignment) and SQL stetement.
>>
>> syntax of assignment
>> :
>> variable := expression
>>
>> expression is independent on variable, so there are not any "default"
>> expression. PL/pgSQL and SQL are independent environments. If you need
>> default values, simply don't change field.
>>
>> Regards
>> Pavel Stehule
>
>