Re: question for serial types with CHECK conditions

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: "Guido Barosio" <gbarosio(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: question for serial types with CHECK conditions
Date: 2007-04-28 16:12:31
Message-ID: 5D828B62-30AA-4C60-92B4-E83E205E0286@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


On Apr 28, 2007, at 10:30 , Guido Barosio wrote:

> Now, my question is: Shouldn't postgresql avoid the creation of the
> table while a serial type contains a check condition?

My question to you is why should it? "a" SERIAL is a shorthand for
creating an INTEGER column "a", a sequence ("a_seq") with a
dependency, and DEFAULT nextval(a_seq). There may be a valid reason
someone wants to put additional constraints on the column, and I'm
not sure why the server should second guess the DBA in this case. If
the CHECK constraint isn't what you want, then don't include it: and
in this case the server helpfully gave you an error which let you
know that the CHECK constraint was not doing what you expected.

Also, the server doesn't have the smarts to look into the CHECK
constraint and decide if it makes sense in your case. For example,
perhaps you want to have CHECK (a > 0), which won't really do
anything for a default sequence. However, if the sequence is changed,
it may return negative integers, which you may not want, so in some
cases, CHECK (a > 0) may be a valid constraint *in your case*.

The crux of the issue is that there may be valid reasons to have a
CHECK constraint on a INTEGER (SERIAL) column, and the server is not
(and will probably never be) smart enough to know your particular
business rules without you telling it specifically.

Does this help clarify the situation?

Michael Glaesemann
grzm seespotcode net

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Guido Barosio 2007-04-28 16:34:15 Re: question for serial types with CHECK conditions
Previous Message shieldy 2007-04-28 15:40:04 SOS, help me please, one problem towards the postgresql develope on windows