# Problem with habtm and resulting SQL insert

**URL:** <https://www.ruby-forum.com/t/problem-with-habtm-and-resulting-sql-insert/52002>\
**Category:** Rails\
**Created:** [January 10, 2006, 12:41am UTC](https://www.ruby-forum.com/t/problem-with-habtm-and-resulting-sql-insert/52002 "2006-01-10T00:41:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![waddilove](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@waddilove](https://www.ruby-forum.com/u/waddilove)\
**Post date:** [January 10, 2006, 12:41am UTC](https://www.ruby-forum.com/t/problem-with-habtm-and-resulting-sql-insert/52002/1 "2006-01-10T00:41:20Z")

</div>

Cheers,

I have a problem with 1.0 and a habtm relationship between User and  
Article.

I want to save all articles that users read. I have these models:

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :read\_articles, :class\_name =\> “Article”,  
:join\_table =\> “read\_articles”  
…  
end

class Article \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :readers, :class\_name =\> “User”, :join\_table  
=\> “read\_articles”  
…  
end

When I do a

a = User.find(1)  
b = Article.find(12)

a.read\_articles \<\< b

The SQL looks like:

INSERT INTO read\_articles (`updated_at`, `id`, `article_id`, `user_id`,  
`created_at`) VALUES (‘2005-12-10 23:26:56’, 12, 12, 1, ‘2005-12-05  
03:44:01’)

The problem is that the `id` field will be set with the article\_id. The  
`id` should be set by the database.

The SQL definition of read\_articles looks like this:

mysql\> describe read\_articles;  
±-----------±---------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±-----------±---------±-----±----±--------±---------------+  
| id | int(11) | | PRI | NULL | auto\_increment |  
| user\_id | int(11) | | | 0 | |  
| article\_id | int(11) | | | 0 | |  
| created\_at | datetime | YES | | NULL | |  
| updated\_at | datetime | YES | | NULL | |  
±-----------±---------±-----±----±--------±---------------+

Any solutions to my problem?

Thanks,  
Jonathan

---

<div class="post-metadata">

**Author:** ![waddilove](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@waddilove](https://www.ruby-forum.com/u/waddilove)\
**Post date:** [January 10, 2006, 12:47am UTC](https://www.ruby-forum.com/t/problem-with-habtm-and-resulting-sql-insert/52002/2 "2006-01-10T00:47:21Z")

</div>

> Any solutions to my problem?

## Yes, don’t have an id field in read\_articles. It’s just a join table, not an entity table. Only entity tables should have ids.

David Heinemeier H.  
[http://www.loudthinking.com](http://www.loudthinking.com) – Broadcasting Brain  
[http://www.basecamphq.com](http://www.basecamphq.com) – Online project management  
[http://www.backpackit.com](http://www.backpackit.com) – Personal information manager  
[http://www.rubyonrails.com](http://www.rubyonrails.com) – Web-application framework

---

<div class="post-metadata">

**Author:** ![waddilove](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@waddilove](https://www.ruby-forum.com/u/waddilove)\
**Post date:** [January 10, 2006, 1:11am UTC](https://www.ruby-forum.com/t/problem-with-habtm-and-resulting-sql-insert/52002/3 "2006-01-10T01:11:27Z")

</div>

On 1/9/06, Jonathan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> INSERT INTO read\_articles (`updated_at`, `id`, `article_id`, `user_id`,  
> `created_at`) VALUES (‘2005-12-10 23:26:56’, 12, 12, 1, ‘2005-12-05  
> 03:44:01’)
> 
> The problem is that the `id` field will be set with the article\_id. The  
> `id` should be set by the database.

The Allow HABTM Primary Key Plugin fixes this (it’s a one line change  
to HasAndBelongsToManyAssociation#insert\_record). See  
[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/Plugins). This is a long  
standing issue with Rails (see  
[http://dev.rubyonrails.org/ticket/1031](http://dev.rubyonrails.org/ticket/1031)). I consider it a major bug,  
but I don’t think everyone would agree.

---

<div class="post-metadata">

**Author:** ![waddilove](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@waddilove](https://www.ruby-forum.com/u/waddilove)\
**Post date:** [January 10, 2006, 5:12am UTC](https://www.ruby-forum.com/t/problem-with-habtm-and-resulting-sql-insert/52002/4 "2006-01-10T05:12:37Z")

</div>

> Why does ActiveRecord set the id to article.id?

## Actually, it doesn’t, your database determines which id makes it out. Thing is that Rails is doing a SELECT \* FROM which includes all the columns from both join table and entity table.

David Heinemeier H.  
[http://www.loudthinking.com](http://www.loudthinking.com) – Broadcasting Brain  
[http://www.basecamphq.com](http://www.basecamphq.com) – Online project management  
[http://www.backpackit.com](http://www.backpackit.com) – Personal information manager  
[http://www.rubyonrails.com](http://www.rubyonrails.com) – Web-application framework

---

<div class="post-metadata">

**Author:** ![waddilove](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@waddilove](https://www.ruby-forum.com/u/waddilove)\
**Post date:** [January 10, 2006, 1:14am UTC](https://www.ruby-forum.com/t/problem-with-habtm-and-resulting-sql-insert/52002/5 "2006-01-10T01:14:30Z")

</div>

David Heinemeier H. wrote:

> > Any solutions to my problem?
> 
> Yes, don’t have an id field in read\_articles. It’s just a join table,  
> not an entity table. Only entity tables should have ids.

Ahh, that makes sense 🙂

I created this table with a migration and forgot that `id` will be  
created automatically if you do not specify `:id => false`.

Why does ActiveRecord set the id to article.id?

Thanks,  
Jonathan
