Hi. I have the following models:
class User < ActiveRecord::Base
has_and_belongs_to_many :rooms
end
class Room < ActiveRecord::Base
has_and_belongs_to_many :users
end
And the test:
require File.dirname(__FILE__) + ''/../test_helper''
class RoomTest < Test::Unit::TestCase
def test_habtm
u1 = User.create
u2 = User.create
r1 = Room.create
r2 = Room.create
r1.users << u1
r1.users << u2
r2.users << u2
end
end
Running "rake test" results in the failure:
1) Error:
test_habtm(RoomTest):
ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry
''2'' for
key 1: INSERT INTO rooms_users (`id`, `user_id`, `room_id`) VALUES (2,
2, 2)
The problem is, that Rails for some reason sets the "id" of the
rooms_users record to 2, rather than letting MySQL autoincrement
handle it.
Is this an error or am I missing something? Thanks.
Morten
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Feb 12, 6:15 pm, "Morten" <use...-Mr43XxmkdKzQT0dZR+AlfA@public.gmane.org> wrote:> 1) Error: > test_habtm(RoomTest): > ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry ''2'' for > key 1: INSERT INTO rooms_users (`id`, `user_id`, `room_id`) VALUES (2, > 2, 2) > > The problem is, that Rails for some reason sets the "id" of the > rooms_users record to 2, rather than letting MySQL autoincrement > handle it.Remove the id column from your join table and all will be well. Rails doesn''t like surrogate keys on join tables, unless you model the join. Ciao, Sheldon. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---