Re: automatic insert of next sequence value?

From: Alfred Perlstein <bright(at)wintelcom(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: automatic insert of next sequence value?
Date: 2000-10-12 17:22:10
Message-ID: 20001012102210.K272@fw.wintelcom.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

* J B Bell <jbbell(at)intergate(dot)ca> [001012 10:13] wrote:
> Hi,
>
> I've recently started with pgsql after having used mysql. Oddly, a problem
> the latter seems to have followed me.
>
> When I set up a field with a default value to use a sequence (with "nextval"),
> It doesn't work automatically. Both using DBI and the pgsql cli, if I do
>
> INSERT INTO foo VALUES (NULL);
>
> or
>
> INSERT INTO foo VALUES ('');
>
> I get an error or null is inserted in the first case, if the field is set
> non-null, or a 0 in the second case.
>
> Of course without this, a good part of the convenince of auto-incrementing is
> defeated. I can work around it by looking up the appropriate value, but then
> I need to do locking as well, which is likely slower and definitely more
> annoying.
>
> Is there some settable option that determines what will cause a default value
> to be inserted? Is this a compile-time option of some sort? I've been
> searching around for a couple of weeks on this, with no luck in the docs. I
> would be very grateful for any assistance.

This is because you're abusing INSERT, the porper way to do insert is
to specifify which columns explicitly:

INSERT INTO foo (col1, col3) VALUES ( 5, 6 );

now, if col2 has a default value it will be set properly.

-Alfred

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Aristide Aragon 2000-10-12 17:23:18 Error creating tables.
Previous Message Tomas Berndtsson 2000-10-12 17:16:39 Re: automatic insert of next sequence value?