I am getting the following error during testing of my application (using the Rails testing framework): Mysql::Error: Duplicate entry '140-2' for key 1: INSERT INTO `accessibilities_member_entities` (`accessibility_id`, `member_entity_id`) VALUES (140, 2) The error occurs when I try to save two model objects where the model has the following HABTM association: has_and_belongs_to_many :member_entities, :class_name => "MemberEntity", :join_table => "accessibilities_member_entities", :foreign_key => "accessibility_id", :association_foreign_key => "member_entity_id" The two objects are distinct from each other and I would therefore expect ActiveRecord to generate a different value for accessibility_id in each row. I would expect member_entity_id to be the same value in each row. There is a compound primary key defined for the join table (accessibility_id + member_entity_id). The accessibility_id and member_entity_id database columns are defined as INT. My debugging has been fruitless and the objects seem fine before the save. Is this an error in the Rails testing framework or Active Record? I am using test fixtures etc to test my code. My application is running on Rails 2.2.2 and MySQL 5.0.51 (for Ubuntu). Any help/ideas much appreciated.
on 2009-02-18 18:54
on 2009-02-18 23:01
Sounds like it's trying to insert (140, 2) twice. Are your fixtures set up with unique ID's for each fixture record? On Feb 18, 12:53 pm, "Lee.Longm...@googlemail.com"
on 2009-02-19 08:12
Thanks for your reply... I have not created a fixture for one of the models that has the HABTM association or for the join table itself, although I do have a fixture for the model on the other side of the HABTM. Is it necessary to create fixtures for each? fyi, the model under test has methods whose job it is to populate the model's table and the join table with reference data, and I am trying to test that these methods work. Hence I saw no need to create fixture data for the model and join table.
on 2009-02-24 16:19
Can you post the code for both models and your test? On Feb 19, 2:10 am, "Lee.Longm...@googlemail.com"