Forum: Ruby on Rails Bug with Postgres and/or HABTM?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
monch1962 (Guest)
on 2005-12-02 08:34
(Received via mailing list)
Hello list,

I think I've found a bug in the way Rails is talking to my Postgres DB
when recording HABTM data.

My join_table looks like this:
id serial primary key,
tablea_id integer references tablea(id),
tableb_id integer references tableb(id),

When Rails writes to join_table, I'd expect it to do something like:
INSERT INTO join_table (tablea_id, tableb_id) values (123,456)
or maybe
INSERT INTO join_table (id, tablea_id, tableb_id) values
(nextval('join_table_seq'), 123, 456)

Instead, it's doing something like
INSERT INTO join_table (id, tablea_id, tableb_id) values (123, 123, 456)
with the result that id isn't being allowed to default to it's correct
serial value, but is instead having the value of tablea_id written to

This has an obvious problem; as soon as I try to post a 2nd record to
join_table where tablea_id=123, I get a duplicate key error.  The
value 123 has already been written (incorrectly) to the id field, and
the 2nd record insertion tries to write a 2nd record with id=123.

I can't believe nobody's run into this already, but equally I can't
find a defect logged for it at and can't find
anything in my code that could be causing it.  If no-one's seen it
already, I'll try to reproduce it in as simple a form as possible and
log a defect for it.

Postgres 8.0.4, running on Gentoo with 2.6.14r2 kernel (for what it's
Rails 0.14.3

Has anyone seen this before?


Dave M.
jeremyevans0 (Guest)
on 2005-12-02 09:15
(Received via mailing list)
On 12/1/05, David M. <removed_email_address@domain.invalid> wrote:
> Has anyone seen this before?

This bug has been known about since April.  See  I have a plugin for 0.14.3
that fixes the issue, assuming your primary key is named id.  Let me
know off list if you are interested in it.
This topic is locked and can not be replied to.