# AR inner join with foreign key cannot get it right

**URL:** <https://www.ruby-forum.com/t/ar-inner-join-with-foreign-key-cannot-get-it-right/205435>\
**Category:** Rails\
**Created:** [April 6, 2011, 3:44pm UTC](https://www.ruby-forum.com/t/ar-inner-join-with-foreign-key-cannot-get-it-right/205435 "2011-04-06T15:44:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 6, 2011, 3:44pm UTC](https://www.ruby-forum.com/t/ar-inner-join-with-foreign-key-cannot-get-it-right/205435/1 "2011-04-06T15:44:32Z")

</div>

I have a simple structure , but I have to use existing primary keys  
from external db, I wrote :

class User \< ActiveRecord::Base # ( uid primary key )  
has\_one :users\_role, :foreign\_key =\> :uid  
has\_one :role, :through =\> :users\_role  
end

class Role \< ActiveRecord::Base # ( rid primary key)  
has\_many :users\_role, :foreign\_key =\> :rid  
has\_many :users, :through =\> :users\_role  
end

class UsersRole \< ActiveRecord::Base # ( uid / rid fields in the  
table and I should not have id key there)  
belongs\_to :role, :primary\_key =\> :rid  
belongs\_to :user, :primary\_key =\> :uid  
end

I can get :  
Role.first.users\_role  
BUT  
Role.first.users raises an error ( using users.id … and  
users\_id keys …)  
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column  
‘users.id’ in ‘on clause’: SELECT `users`.\* FROM `users` INNER JOIN  
`users_roles` ON `users`.id = `users_roles`.user\_id WHERE  
((`users_roles`.rid = NULL))

what’s wrong in my writing ?

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [April 6, 2011, 3:50pm UTC](https://www.ruby-forum.com/t/ar-inner-join-with-foreign-key-cannot-get-it-right/205435/2 "2011-04-06T15:50:44Z")

</div>

Is there any reason why you do not use the standard FK names so you do  
not  
have to tell it what they are?

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [April 6, 2011, 4:00pm UTC](https://www.ruby-forum.com/t/ar-inner-join-with-foreign-key-cannot-get-it-right/205435/3 "2011-04-06T16:00:01Z")

</div>

On Wed, Apr 6, 2011 at 8:43 AM, Erwin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :users, :through =\> :users\_role  
> BUT  
> Role.first.users raises an error ( using users.id … and  
> users\_id keys ..)  
> ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column  
> ‘users.id’ in ‘on clause’: SELECT `users`.\* FROM `users` INNER JOIN  
> `users_roles` ON `users`.id = `users_roles`.user\_id WHERE  
> ((`users_roles`.rid = NULL))
> 
> what’s wrong in my writing ?

You’re missing a sorce on your has\_many :users, :through =\>  
:users\_role in  
model Role. It’s trying to use :role in model UserRole because that is  
what  
the has\_many :users\_role, :foreign\_key =\> :rid from model Role is set  
to  
use. Do the following:

Change this:

:users, :through =\> :users\_role

To this:

:users, :through =\> :users\_role, :source =\> :user

That should fix your issue. Let us know how it goes.

B.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [April 6, 2011, 4:00pm UTC](https://www.ruby-forum.com/t/ar-inner-join-with-foreign-key-cannot-get-it-right/205435/4 "2011-04-06T16:00:01Z")

</div>

On 6 April 2011 14:43, Erwin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a simple structure , but I have to use existing primary keys  
> from external db, I wrote :
> 
> class User \< ActiveRecord::Base # ( uid primary key )

You need to use set\_primary\_key here to tell rails that the primary  
key of the users\_table is uid

> has\_one :users\_role, :foreign\_key =\> :uid  
> has\_one :role, :through =\> :users\_role  
> end
> 
> class Role \< ActiveRecord::Base # ( rid primary key)

Again you need set\_primary\_key

> has\_many :users\_role, :foreign\_key =\> :rid  
> has\_many :users, :through =\> :users\_role  
> end
> 
> class UsersRole \< ActiveRecord::Base # ( uid / rid fields in the  
> table and I should not have id key there)  
> belongs\_to :role, :primary\_key =\> :rid  
> belongs\_to :user, :primary\_key =\> :uid

Those should be foreign\_key I think. I am not sure how you tell rails  
that there is no primary key for this table.

> end
> 
> I can get :  
> Role.first.users\_role  
> BUT  
> Role.first.users raises an error ( using users.id … and  
> users\_id keys ..)  
> ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column  
> ‘users.id’ in ‘on clause’: SELECT `users`.\* FROM `users` INNER JOIN

The error is because it thinks users has an id field, as you did not  
set\_primary\_key

Colin
