Bug with Postgres and/or HABTM?

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),
other_attributes…

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
it.

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 dev.rubyonrails.com 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
worth)
Rails 0.14.3

Has anyone seen this before?

Regards

Dave M.

On 12/1/05, David M. [email protected] wrote:

Has anyone seen this before?

This bug has been known about since April. See
http://dev.rubyonrails.org/ticket/1031. 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.