# Habtm problem

**URL:** <https://www.ruby-forum.com/t/habtm-problem/159269>\
**Category:** Rails\
**Created:** [February 2, 2009, 9:58pm UTC](https://www.ruby-forum.com/t/habtm-problem/159269 "2009-02-02T21:58:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![littleknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc29f/32.png) [@littleknown](https://www.ruby-forum.com/u/littleknown)\
**Post date:** [February 2, 2009, 9:58pm UTC](https://www.ruby-forum.com/t/habtm-problem/159269/1 "2009-02-02T21:58:43Z")

</div>

I have Kids and I have Sports.

I have a checkbox list of Sports for each Kid.

Let’s say I make a Kid, and assign him to all 5 sports.

It works, great.

Let’s say I make another Kid(Kid2), and assign him to all 5 sports. I  
get this error:

Mysql::Error: Duplicate entry ‘2’ for key 1: INSERT INTO kids\_sports  
(`sport_id`, `kid_id`, `id`) VALUES (2, 10, 2)

And my kids\_sports table looks like this:

```
                   id kid_id sport_id
Edit Delete 2 8 2

```

Edit Delete 3 8 3  
Edit Delete 4 8 4  
Edit Delete 6 8 6  
Edit Delete 7 8 7

Why the heck is it doing that? Why isn’t id 1,2,3,4,5 etc like it  
should(?) be?

---

<div class="post-metadata">

**Author:** ![littleknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc29f/32.png) [@littleknown](https://www.ruby-forum.com/u/littleknown)\
**Post date:** [February 2, 2009, 10:37pm UTC](https://www.ruby-forum.com/t/habtm-problem/159269/2 "2009-02-02T22:37:06Z")

</div>

I think we need to see one your kid/sport model and then the new/  
create method in the controller…

You should not have a id column btw in the kids\_sports table… .

This should be your up task:

def self.up  
create\_table :kids\_sports, :id =\> false do |t|  
t.integer :kid\_id  
t.integer :sport\_id  
end  
add\_index :kids\_sports, [:kid\_id]  
add\_index :kids\_sports, [:sport\_id]  
end

On Feb 2, 12:58 pm, Little K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
