Forum: Ruby on Rails Mysql::Error: Duplicate entry '#' for key # - Error with the test framework or ActiveRecord???

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
753038ae16700c0fd9a5e1d2e80f68f6?d=identicon&s=25 Lee.Longmore@googlemail.com (Guest)
on 2009-02-18 18:54
(Received via mailing list)
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.
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2009-02-18 23:01
(Received via mailing list)
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"
753038ae16700c0fd9a5e1d2e80f68f6?d=identicon&s=25 Lee.Longmore@googlemail.com (Guest)
on 2009-02-19 08:12
(Received via mailing list)
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.
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2009-02-24 16:19
(Received via mailing list)
Can you post the code for both models and your test?


On Feb 19, 2:10 am, "Lee.Longm...@googlemail.com"
This topic is locked and can not be replied to.