# Foreign Key naming convention override

**URL:** <https://www.ruby-forum.com/t/foreign-key-naming-convention-override/93733>\
**Category:** Rails\
**Created:** [April 7, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/foreign-key-naming-convention-override/93733 "2006-04-07T18:18:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 7, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/foreign-key-naming-convention-override/93733/1 "2006-04-07T18:18:36Z")

</div>

I am working with an existing database and have to keep all table and  
field names as they are. ‘tblcustomers’ and ‘tblitems’ are linked by the  
‘tblcustomers’ id field ‘tblcustomersid’. In ‘tblitems’, the foreign key  
is called ‘txtcustomerid’.

I have already set the customers model to override the ‘id’ naming  
convention for the primary key by adding the following to the customers  
model.

```
 set_primary_key(:intcustomerid)

```

How do I tell rails to override the preferred name for the foreign key  
‘txtcustomerid’? I have tried:

```
 set_foreign_key(:intcustomerid) and :foreign_key => "intcustomerid"

```

but neither work. I’m running out of ideas!

Thanks,  
Alana

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 10, 2006, 10:27am UTC](https://www.ruby-forum.com/t/foreign-key-naming-convention-override/93733/2 "2006-04-10T10:27:16Z")

</div>

Hi,

Can anyone help with this problem?

Alana

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 10, 2006, 10:39am UTC](https://www.ruby-forum.com/t/foreign-key-naming-convention-override/93733/3 "2006-04-10T10:39:36Z")

</div>

Alana wrote:

> Hi,
> 
> Can anyone help with this problem?
> 
> Alana

You define custom foreign key when you define the relationship between  
the tables in your model file.

From page 231 of Agile Web D. With Rails:

class LineItem \< ActiveRecord::Base  
belongs\_to :paid\_order,  
:class\_name =\> "Order,  
:foreign\_key =\> “order\_id”,  
:conditions =\> “paid\_on is not null”  
end

so in your example you might say:

class Item \< ActiveRecord::Base  
set\_table\_name “tblitems”  
belongs\_to :customer,  
:foreign\_key =\> “txtcustomersid”  
end

and

class Customer \< ActiveRecord::Base  
set\_table\_name “tblcustomers”  
belongs\_to :items,  
:foreign\_key =\> “tblcustomerid”  
end

Something like that.

Jeff

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 10, 2006, 12:57pm UTC](https://www.ruby-forum.com/t/foreign-key-naming-convention-override/93733/4 "2006-04-10T12:57:11Z")

</div>

Hi Jeff,

Thanks for your reply. How can the class be “Customer” when the table is  
called “tblcustomers”? should the class not be set to “Tblcustomers”?

> class Customer \< ActiveRecord::Base  
> set\_table\_name “tblcustomers”  
> belongs\_to :items,  
> :foreign\_key =\> “tblcustomerid”  
> end

Thanks,  
Alana

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 10, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/foreign-key-naming-convention-override/93733/5 "2006-04-10T16:33:34Z")

</div>

Alana wrote:

> Hi Jeff,
> 
> Thanks for your reply. How can the class be “Customer” when the table is  
> called “tblcustomers”? should the class not be set to “Tblcustomers”?
> 
> > class Customer \< ActiveRecord::Base  
> > set\_table\_name “tblcustomers”  
> > belongs\_to :items,  
> > :foreign\_key =\> “tblcustomerid”  
> > end

The code above says that the class Customer is mapped to the table  
‘tblcustomers’ thanks to the set\_table\_name method, no ?

regards,

Barth

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 10, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/foreign-key-naming-convention-override/93733/6 "2006-04-10T19:04:46Z")

</div>

Alana wrote:

> Hi Jeff,
> 
> Thanks for your reply. How can the class be “Customer” when the table is  
> called “tblcustomers”? should the class not be set to “Tblcustomers”?
> 
> > class Customer \< ActiveRecord::Base  
> > set\_table\_name “tblcustomers”  
> > belongs\_to :items,  
> > :foreign\_key =\> “tblcustomerid”  
> > end
> 
> Thanks,  
> Alana

The “set\_table\_name” property lets you work with a legacy schema but  
still have readable code and class names.

Oh, and I made a mistake in the above code. I believe what you’re  
describing for Customer and Item is a has\_many relationship, where  
Customer has\_many Items…

class Customer \< ActiveRecord::Base  
set\_table\_name “tblcustomers”  
has\_many :items,  
:foreign\_key =\> “tblcustomerid”  
end

Jeff C.man

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 10, 2006, 10:42am UTC](https://www.ruby-forum.com/t/foreign-key-naming-convention-override/93733/7 "2006-04-10T10:42:48Z")

</div>

Try here:  
[http://wiki.rubyonrails.com/rails/pages/HowToUseLegacySchemas](http://wiki.rubyonrails.com/rails/pages/HowToUseLegacySchemas)

Alana wrote:

> I am working with an existing database and have to keep all table and  
> field names as they are. ‘tblcustomers’ and ‘tblitems’ are linked by the  
> ‘tblcustomers’ id field ‘tblcustomersid’. In ‘tblitems’, the foreign key  
> is called ‘txtcustomerid’.
> 
> I have already set the customers model to override the ‘id’ naming  
> convention for the primary key by adding the following to the customers  
> model.
> 
> ```
> set_primary_key(:intcustomerid)
> 
> ```
> 
> How do I tell rails to override the preferred name for the foreign key  
> ‘txtcustomerid’? I have tried:
> 
> ```
> set_foreign_key(:intcustomerid) and :foreign_key => "intcustomerid"
> 
> ```
> 
> but neither work. I’m running out of ideas!
> 
> Thanks,  
> Alana
