Re: PostgreSQL primary (sequence) key issue (Ruby/Rails) - Mailing list pgsql-general

From Russ Brown
Subject Re: PostgreSQL primary (sequence) key issue (Ruby/Rails)
Date
Msg-id 45E72EC2.2020702@gmail.com
Whole thread Raw
In response to Re: PostgreSQL primary (sequence) key issue (Ruby/Rails)  ("Joshua D. Drake" <jd@commandprompt.com>)
Responses Re: PostgreSQL primary (sequence) key issue (Ruby/Rails)  (Scott Ribe <scott_ribe@killerbytes.com>)
List pgsql-general
Joshua D. Drake wrote:
>> On 01/03/07, Andrew Madu < andrewmadu@gmail.com> wrote:
>>> Hi Dave,
>>> my apologies for contacting you off list but i'm having a spot of bother
>>> with postgreSQL sequence setup in rails. In addition to what is mentioned
>>> below, I have place the following line of code in my
>
> The definition of primary key explicitly states that it can't be null.
> You are trying to pass a null to user_id which won't work.
>
> Joshua D. Drkae
>

In MySQL that is traditionally how you tell the RDBMS to use the
auto_increment to generate the value. Postgres correctly doesn't allow
that (since you might actually try to set a field to NULL accidentally
in which case an error is expected).

The portable (and correct) way to do it is to use the DEFAULT keyword
like this:

INSERT INTO some_table (id_field) VALUES (DEFAULT);

I just tested on MySQL 5.0.32 and that syntax works fine.

--

Russ.

pgsql-general by date:

Previous
From: "Joshua D. Drake"
Date:
Subject: Re: PostgreSQL primary (sequence) key issue (Ruby/Rails)
Next
From: Alan Hodgson
Date:
Subject: hardware question - LSI MegaRaid 8480e