# HABTM join table has an "ID" column - is this an issue?

**URL:** <https://www.ruby-forum.com/t/habtm-join-table-has-an-id-column-is-this-an-issue/97245>\
**Category:** Rails\
**Created:** [July 5, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/habtm-join-table-has-an-id-column-is-this-an-issue/97245 "2006-07-05T22:38:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 5, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/habtm-join-table-has-an-id-column-is-this-an-issue/97245/1 "2006-07-05T22:38:14Z")

</div>

All,

I’m building model objects for existing tables that I cannot modify.

In AWDWR, Dave says “Note that our join table has no id column…The  
second reason for not including an id column in the join table is that  
AR automatically includes all columns from the join tables when  
accessing rows using it. If the join table included a column called id,  
its id would overwrite the id of the rows in the joined tables.”

Unfortunately, my join table does have a column called id. Is there a  
directive that can help me with this or some way to get around it per  
Dave T.'s quote above?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 5, 2006, 10:45pm UTC](https://www.ruby-forum.com/t/habtm-join-table-has-an-id-column-is-this-an-issue/97245/2 "2006-07-05T22:45:24Z")

</div>

Wes G. wrote:

> All,
> 
> I’m building model objects for existing tables that I cannot modify.
> 
> In AWDWR, Dave says “Note that our join table has no id column…The  
> second reason for not including an id column in the join table is that  
> AR automatically includes all columns from the join tables when  
> accessing rows using it. If the join table included a column called id,  
> its id would overwrite the id of the rows in the joined tables.”
> 
> Unfortunately, my join table does have a column called id. Is there a  
> directive that can help me with this or some way to get around it per  
> Dave T.'s quote above?
> 
> Thanks,  
> Wes

Should I provide a value for the :select option, which will keep a  
SELECT \* from happening on my join table?

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 5, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/habtm-join-table-has-an-id-column-is-this-an-issue/97245/3 "2006-07-05T23:20:06Z")

</div>

You could upgrade the join table to a full model and use has\_many  
:through in place of habtm.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 5, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/habtm-join-table-has-an-id-column-is-this-an-issue/97245/4 "2006-07-05T23:22:40Z")

</div>

Tom W. wrote:

> You could upgrade the join table to a full model and use has\_many  
> :through in place of habtm.

I just realized, however, that although my join table has an “id”  
column, neither of the join\_ed\_ tables on either side of the association  
have “id” columns.

So, I’m thinking that there shouldn’t be any collision between columns  
here.

Where can I find out more about has\_many :through? - I’m unfamiliar with  
this option.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 5, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/habtm-join-table-has-an-id-column-is-this-an-issue/97245/5 "2006-07-05T23:32:51Z")

</div>

On 7/5/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Where can I find out more about has\_many :through? - I’m unfamiliar with  
> this option.

[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000530](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000530)

[http://wiki.rubyonrails.org/rails/pages/ThroughAssociations](http://wiki.rubyonrails.org/rails/pages/ThroughAssociations)

Tom

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 6, 2006, 5:08am UTC](https://www.ruby-forum.com/t/habtm-join-table-has-an-id-column-is-this-an-issue/97245/6 "2006-07-06T05:08:47Z")

</div>

Wes G. wrote:

> Tom W. wrote:
> 
> > You could upgrade the join table to a full model and use has\_many  
> > :through in place of habtm.
> 
> I just realized, however, that although my join table has an “id”  
> column, neither of the join\_ed\_ tables on either side of the association  
> have “id” columns.
> 
> So, I’m thinking that there shouldn’t be any collision between columns  
> here.
> 
> Where can I find out more about has\_many :through? - I’m unfamiliar with  
> this option.

You shouldn’t have an “id” column in a join table - that will just get  
you in trouble. If you have anything in your join table besides the  
foreign\_keys of the two tables being joined, you’ll have problems. In  
that case, you’ll want to use a join model (has\_many :through) instead.

[http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off](http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 6, 2006, 6:04am UTC](https://www.ruby-forum.com/t/habtm-join-table-has-an-id-column-is-this-an-issue/97245/7 "2006-07-06T06:04:23Z")

</div>

Josh S. wrote:

> Wes G. wrote:
> 
> > Tom W. wrote:
> > 
> > > You could upgrade the join table to a full model and use has\_many  
> > > :through in place of habtm.
> > 
> > I just realized, however, that although my join table has an “id”  
> > column, neither of the join\_ed\_ tables on either side of the association  
> > have “id” columns.
> > 
> > So, I’m thinking that there shouldn’t be any collision between columns  
> > here.
> > 
> > Where can I find out more about has\_many :through? - I’m unfamiliar with  
> > this option.
> 
> You shouldn’t have an “id” column in a join table - that will just get  
> you in trouble. If you have anything in your join table besides the  
> foreign\_keys of the two tables being joined, you’ll have problems. In  
> that case, you’ll want to use a join model (has\_many :through) instead.
> 
> [http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off](http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off)
> 
> –  
> Josh S.  
> [http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

Will I have problems if _neither_ of the two tables in the HABTM  
relationship has an “id” column?

WG

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 6, 2006, 6:13am UTC](https://www.ruby-forum.com/t/habtm-join-table-has-an-id-column-is-this-an-issue/97245/8 "2006-07-06T06:13:26Z")

</div>

Wes G. wrote:

> Will I have problems if _neither_ of the two tables in the HABTM  
> relationship has an “id” column?

ActiveRecord requires a primary key to manage object identity and to do  
joins between tables. If you have a primary key but have renamed the  
column from “id” to something else, you’ll be fine. If not, you’ll need  
to change something, or habtm won’t have anything to work with to do the  
joins.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
