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)
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 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
Has anyone seen this before?