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.