# Foreign\_key setting ignored

**URL:** <https://www.ruby-forum.com/t/foreign-key-setting-ignored/50907>\
**Category:** Rails\
**Created:** [December 22, 2005, 5:17pm UTC](https://www.ruby-forum.com/t/foreign-key-setting-ignored/50907 "2005-12-22T17:17:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lofye](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bb73d2/32.png) [@lofye](https://www.ruby-forum.com/u/lofye)\
**Post date:** [December 22, 2005, 5:17pm UTC](https://www.ruby-forum.com/t/foreign-key-setting-ignored/50907/1 "2005-12-22T17:17:08Z")

</div>

I’m having a spot of bother creating a ‘has\_many’ relationship between  
two legacy database tables. The relationship is ‘company’ table has many  
linked records in the ‘contact’ table. Iv’e used ‘generate scaffold’ on  
these tables and everything is fine. I then decided to define the  
relationship in the relevant model classes…

## class Contact \< ActiveRecord::Base belongs\_to :company,:foreign\_key =\> “companyid” end

class Company \< ActiveRecord::Base  
has\_many :contact  
end

And then I’ve added the following line to views/company/show.rhtml to  
try and get at the child elements…

\<%= debug(@company.contact) %\>

I’ve tried loads of variations on this setup, but always end up with an  
error along the lines of…

|“invalid identifier: SELECT \* FROM contact WHERE (contact.company\_id = 100011847)”

|Rails appears to be ignoring the foreign\_key setting in contact.rb and attempts to use company\_id instead of companyid. Can anyone suggest what’s wrong with this setup?

Thanks in advance for your help,  
Derek

---

<div class="post-metadata">

**Author:** ![lofye](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bb73d2/32.png) [@lofye](https://www.ruby-forum.com/u/lofye)\
**Post date:** [December 22, 2005, 5:23pm UTC](https://www.ruby-forum.com/t/foreign-key-setting-ignored/50907/2 "2005-12-22T17:23:08Z")

</div>

On 12/22/05, Derek M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Company \< ActiveRecord::Base
> 
> |“invalid identifier: SELECT \* FROM contact WHERE (contact.company\_id = 100011847)”
> 
> |Rails appears to be ignoring the foreign\_key setting in contact.rb and attempts to use company\_id instead of companyid. Can anyone suggest what’s wrong with this setup?

If your table is actually called “contact” instead of “contacts”,  
you’ll also need to override that. ActiveRecord is looking for a file  
with a different name than ‘contact.rb’, and not finding it.  
So, try:  
has\_many :contact, :class\_name =\> ‘Contact’

Alternately, you could rename the table and say:  
has\_many :contacts

---

<div class="post-metadata">

**Author:** ![lofye](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bb73d2/32.png) [@lofye](https://www.ruby-forum.com/u/lofye)\
**Post date:** [December 22, 2005, 5:29pm UTC](https://www.ruby-forum.com/t/foreign-key-setting-ignored/50907/3 "2005-12-22T17:29:10Z")

</div>

Derek M. wrote:

> I’m having a spot of bother creating a ‘has\_many’ relationship between  
> two legacy database tables. The relationship is ‘company’ table has many  
> linked records in the ‘contact’ table. Iv’e used ‘generate scaffold’ on  
> these tables and everything is fine. I then decided to define the  
> relationship in the relevant model classes…

I would be more verbose for those legacy tables:

class Contact \< ActiveRecord::Base  
set\_table\_name ‘contact’ # unless pluralize\_table\_names if off  
belongs\_to :company,:foreign\_key =\> “companyid”  
end

class Company \< ActiveRecord::Base  
set\_table\_name ‘company’  
has\_many :contacts, :class\_name =\> ‘Contact’, :foreign\_key =\>  
‘companyid’  
end

---

<div class="post-metadata">

**Author:** ![lofye](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bb73d2/32.png) [@lofye](https://www.ruby-forum.com/u/lofye)\
**Post date:** [January 3, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/foreign-key-setting-ignored/50907/4 "2006-01-03T16:17:09Z")

</div>

Lugovoi N. wrote:

> Derek M. wrote:
> 
> > I’m having a spot of bother creating a ‘has\_many’ relationship between  
> > two legacy database tables. The relationship is ‘company’ table has many  
> > linked records in the ‘contact’ table. Iv’e used ‘generate scaffold’ on  
> > these tables and everything is fine. I then decided to define the  
> > relationship in the relevant model classes…
> 
> I would be more verbose for those legacy tables:
> 
> class Contact \< ActiveRecord::Base  
> set\_table\_name ‘contact’ # unless pluralize\_table\_names if off  
> belongs\_to :company,:foreign\_key =\> “companyid”  
> end
> 
> class Company \< ActiveRecord::Base  
> set\_table\_name ‘company’  
> has\_many :contacts, :class\_name =\> ‘Contact’, :foreign\_key =\>  
> ‘companyid’  
> end

It’s now fixed, thanks for the help! If you’re interested…

contact.rb didn’t need changed and I’d already turned  
pluralize\_table\_names off and added  
ActiveRecord::Base.primary\_key\_prefix\_type=:table\_name  
to environment.rb

I’d tried being verbose and setting the foreign\_key in company.rb as  
you’ve suggested, but it was the additional  
:class\_name =\> ‘Contact’  
that fixed the has\_many relationship for me 🙂
