# HABTM - primary key problem

**URL:** <https://www.ruby-forum.com/t/habtm-primary-key-problem/171472>\
**Category:** Rails\
**Created:** [July 3, 2009, 6:24am UTC](https://www.ruby-forum.com/t/habtm-primary-key-problem/171472 "2009-07-03T06:24:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiahong\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/3d9bf3/32.png) [@Xiahong\_G](https://www.ruby-forum.com/u/Xiahong_G)\
**Post date:** [July 3, 2009, 6:24am UTC](https://www.ruby-forum.com/t/habtm-primary-key-problem/171472/1 "2009-07-03T06:24:08Z")

</div>

Hi everyone,

I ran into a serious problem when I was trying to do something similar  
to twitter follow mechanism. The thing is like this:

First, I have a self-referential table, named “users”  
create\_table :users do |t|  
t.column :name, :string  
t.column :user\_name, :string  
t.column :password, :string  
t.column :email, :string  
end

I also have a HABTM join table, named “follow\_relations”  
create\_table :follow\_realtions do |t|  
t.column :a\_id, :integer # a\_id follows b\_id  
t.column :b\_id, :integer  
end

My HABTM join is defined as follows  
has\_and\_belongs\_to\_many :followers,  
:class\_name =\>  
“User”,  
:join\_table =\>  
“follow\_relations”,  
:foreign\_key =\>  
“b\_id”,  
:association\_foreign\_key  
=\> “a\_id”

has\_and\_belongs\_to\_many :followings,  
:class\_name =\> “User”,  
:join\_table =\>  
“follow\_relations”,  
:foreign\_key =\>  
“a\_id”,  
:association\_foreign\_key  
=\> “b\_id”

When I executed:  
gaoxh = User.create(:name =\> “gaoxh”)  
micai = User.create(:name =\> “micai”)  
xiexin = User.create(:name =\> “xiexin”)  
gaoxh.followers \<\< micai  
xiexin.followers \<\< micai

The first \<\< operation executes the following SQL :  
INSERT INTO `follow_relations (`id`,`b\_id`,`a\_id`) VALUES (3, 1, 3) The second << operation executes the following SQL: INSERT INTO`follow\_relations (`id`, `b_id`, `a_id`) VALUES (3, 2, 3)  
Thus, key id is duplicated. It looks like that every time, the value  
of column id is copied from column a\_id.  
What’s going on here? How to fix this problem?

Thanks in advance,  
xiahong

---

<div class="post-metadata">

**Author:** ![Xiahong\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/3d9bf3/32.png) [@Xiahong\_G](https://www.ruby-forum.com/u/Xiahong_G)\
**Post date:** [July 3, 2009, 10:21am UTC](https://www.ruby-forum.com/t/habtm-primary-key-problem/171472/2 "2009-07-03T10:21:52Z")

</div>

If I remove id column, what is the primary key?

2009/7/3 Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Xiahong\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/3d9bf3/32.png) [@Xiahong\_G](https://www.ruby-forum.com/u/Xiahong_G)\
**Post date:** [July 3, 2009, 9:48am UTC](https://www.ruby-forum.com/t/habtm-primary-key-problem/171472/3 "2009-07-03T09:48:30Z")

</div>

Xiahong G. wrote:

> Hi everyone,
> 
> I ran into a serious problem when I was trying to do something similar  
> to twitter follow mechanism. The thing is like this:
> 
> First, I have a self-referential table, named “users”  
> create\_table :users do |t|  
> t.column :name, :string  
> t.column :user\_name, :string  
> t.column :password, :string  
> t.column :email, :string  
> end
> 
> I also have a HABTM join table, named “follow\_relations”  
> create\_table :follow\_realtions do |t|  
> t.column :a\_id, :integer # a\_id follows b\_id  
> t.column :b\_id, :integer  
> end
> 
> My HABTM join is defined as follows  
> has\_and\_belongs\_to\_many :followers,  
> :class\_name =\>  
> “User”,  
> :join\_table =\>  
> “follow\_relations”,  
> :foreign\_key =\>  
> “b\_id”,  
> :association\_foreign\_key  
> =\> “a\_id”
> 
> has\_and\_belongs\_to\_many :followings,  
> :class\_name =\> “User”,  
> :join\_table =\>  
> “follow\_relations”,  
> :foreign\_key =\>  
> “a\_id”,  
> :association\_foreign\_key  
> =\> “b\_id”  
> […]  
> The first \<\< operation executes the following SQL :  
> INSERT INTO `follow_relations (`id`, `b\_id`, `a\_id`) VALUES (3, 1, 3) The second << operation executes the following SQL: INSERT INTO `follow\_relations (`id`, `b_id`, `a_id`) VALUES (3, 2, 3)  
> Thus, key id is duplicated. It looks like that every time, the value  
> of column id is copied from column a\_id.  
> What’s going on here? How to fix this problem?

If you’re using HABTM, the join table (follow\_relations) should not have  
an id column. Remove it and see if that does the trick.

> Thanks in advance,  
> xiahong

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Xiahong\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/3d9bf3/32.png) [@Xiahong\_G](https://www.ruby-forum.com/u/Xiahong_G)\
**Post date:** [July 3, 2009, 10:30am UTC](https://www.ruby-forum.com/t/habtm-primary-key-problem/171472/4 "2009-07-03T10:30:03Z")

</div>

2009/7/3 gaoxh gaoxh04 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> If I remove id column, what is the primary key?

Have a look at the rails guides, particularly the one on ActiveRecord  
associations at [Ruby on Rails Guides](http://guides.rubyonrails.org/index.html)

Colin

---

<div class="post-metadata">

**Author:** ![Xiahong\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/3d9bf3/32.png) [@Xiahong\_G](https://www.ruby-forum.com/u/Xiahong_G)\
**Post date:** [July 3, 2009, 2:27pm UTC](https://www.ruby-forum.com/t/habtm-primary-key-problem/171472/5 "2009-07-03T14:27:49Z")

</div>

thanks all… problem solved.

2009/7/3 Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
