# Has and belongs to many, join table id included in query

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-join-table-id-included-in-query/150934>\
**Category:** Rails\
**Created:** [October 15, 2008, 12:26am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-join-table-id-included-in-query/150934 "2008-10-15T00:26:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [October 15, 2008, 12:26am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-join-table-id-included-in-query/150934/1 "2008-10-15T00:26:36Z")

</div>

I’m using a habtm association for the first time and I’m having some  
trouble when inserting several rows. The id of the join table seems to  
be set to an already existing id causing the insert to fail.

class ClassA \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :bs,  
:class\_name =\> “ClassB”,  
:join\_table =\> :abs

end

class ClassB \< ActiveRecord::Base  
belongs\_to :class\_a  
end

My join table migration:

def self.up  
create\_table :abs do |t|  
t.integer :class\_a\_id, :null =\> false  
t.integer :class\_b\_id, :null =\> false  
end

```
add_index :abs, [:class_a_id, :class_b_id], :unique => true

```

end

Then finally in the controller:

@a.bs = ClassB.find params[:bs]  
@a.save

Now, this works the first time, but the second time I get this back:

Mysql::Error: #23000Duplicate entry ‘1’ for key 1: INSERT INTO `abs`  
(`class_a_id`, `id`, `class_b_id`) VALUES (7, 1, 1)

Now, I wonder - why is the id included in the query with a value that is  
already used? I’m sure it’s just something I’m doing wrong, but I’m  
having a hard time figuring out what. I looked at the :insert\_sql option  
to habtm too, but don’t know how to include the ids dynamically?

---

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [October 15, 2008, 12:48am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-join-table-id-included-in-query/150934/2 "2008-10-15T00:48:25Z")

</div>

On Oct 14, 11:26 pm, Christian J. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> I’m using a habtm association for the first time and I’m having some  
> trouble when inserting several rows. The id of the join table seems to  
> be set to an already existing id causing the insert to fail.

habtm join tables should not have an id column (use :id =\> false as an  
option to create\_table)

Fred

---

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [October 15, 2008, 1:53am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-join-table-id-included-in-query/150934/3 "2008-10-15T01:53:22Z")

</div>

Not to pimp my blog or anything, but I wrote about this exact issue  
here: [http://bkocik.net/2007/07/26/habtm-and-the-id-field/](http://bkocik.net/2007/07/26/habtm-and-the-id-field/)

That should help you understand what’s going on (in a very long-winded  
manner). 🙂

On Oct 14, 6:26 pm, Christian J. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![chrisjo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chrisjo](https://www.ruby-forum.com/u/chrisjo)\
**Post date:** [October 15, 2008, 11:15am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-join-table-id-included-in-query/150934/4 "2008-10-15T11:15:42Z")

</div>

Bill K. wrote:

> Not to pimp my blog or anything, but I wrote about this exact issue  
> here: [http://bkocik.net/2007/07/26/habtm-and-the-id-field/](http://bkocik.net/2007/07/26/habtm-and-the-id-field/)
> 
> That should help you understand what’s going on (in a very long-winded  
> manner). 🙂
> 
> On Oct 14, 6:26ï¿½pm, Christian J. \<rails-mailing-l…@andreas-

Ah, great stuff, thanks!
