Re: PostgreSQL primary (sequence) key issue (Ruby/Rails)

From: Russ Brown <pickscrape(at)gmail(dot)com>
To: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
Cc: Andrew Madu <andrewmadu(at)gmail(dot)com>, rubyonrails-talk(at)googlegroups(dot)com, pgsql-general(at)postgresql(dot)org
Subject: Re: PostgreSQL primary (sequence) key issue (Ruby/Rails)
Date: 2007-03-01 19:51:30
Message-ID: 45E72EC2.2020702@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Joshua D. Drake wrote:
>> On 01/03/07, Andrew Madu < andrewmadu(at)gmail(dot)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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jerry Sievers 2007-03-01 20:03:41 Connections over SSH tunnels closing after idle time
Previous Message Joshua D. Drake 2007-03-01 19:23:03 Re: PostgreSQL primary (sequence) key issue (Ruby/Rails)