# Has\_and\_belongs\_to\_many question

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-question/113479>\
**Category:** Rails\
**Created:** [August 31, 2007, 2:49pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/113479 "2007-08-31T14:49:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mariko](https://www.ruby-forum.com/u/mariko)\
**Post date:** [August 31, 2007, 2:49pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/113479/1 "2007-08-31T14:49:08Z")

</div>

So I’m playing around with models right now, and according to the Agile  
Web D. with Rails book, this should express a many to many  
relationship:

class Request \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
end

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :requests  
end

I’ve also created a table called requests\_users with columns “user\_id”  
and “request\_id.”

however, when I do this:

request1.users \<\< user  
request2.users \<\< user

I get the error “Mysql::Error: Duplicate entry ‘191’ for key 1.”

What’s going on here? Shouldn’t a user be able to be associated with  
more than one request?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![mariko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mariko](https://www.ruby-forum.com/u/mariko)\
**Post date:** [August 31, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/113479/2 "2007-08-31T14:55:41Z")

</div>

On 8/31/07, Mariko C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_and\_belongs\_to\_many :requests  
> I get the error “Mysql::Error: Duplicate entry ‘191’ for key 1.”
> 
> What’s going on here? Shouldn’t a user be able to be associated with  
> more than one request?

Looks like you’ve defined user\_id as either PRIMARY KEY or UNIQUE in  
the database, which you shouldn’t do. The book shows you how to use a  
migration to create the join table.

---

<div class="post-metadata">

**Author:** ![mariko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mariko](https://www.ruby-forum.com/u/mariko)\
**Post date:** [August 31, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/113479/3 "2007-08-31T16:21:57Z")

</div>

I had this problem a while ago. IIRC, Rails does not automatically  
create a  
primary key column on HABTM join tables the way it does for a Users  
table by  
creating a user\_id column. You can verify this by executing a ‘SHOW  
COLUMNS  
FROM requests\_users;’ in mysql and checking that there is no  
requests\_users\_id column.

The solution is to do what the previous poster suggested and create a  
migration for the join table. In the migration, you should specify the  
primary key column and also add indexes on request\_id and user\_id (as  
appropriate):

class CreateRequestsUsers \< ActiveRecord::Migration  
def self.up  
create\_table :requests\_users do |t|  
t.column :request\_id, :integer, :allow\_null =\> false  
t.column :user\_id, :integer, :allow\_null =\> false  
t.column :request\_user\_id, :integer, :allow\_null =\> false  
t.primary\_key :request\_user\_id  
end  
add\_index :requests\_users, :request\_id  
add\_index :requests\_users, :user\_id  
end

def self.down  
remove\_index :requests\_users, :request\_id  
remove\_index :requests\_users, :user\_id  
drop\_table :requests\_users  
end  
end

-Dan
