# Set the foreign key constraint column name?

**URL:** <https://www.ruby-forum.com/t/set-the-foreign-key-constraint-column-name/51582>\
**Category:** Rails\
**Created:** [January 3, 2006, 9:44pm UTC](https://www.ruby-forum.com/t/set-the-foreign-key-constraint-column-name/51582 "2006-01-03T21:44:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [January 3, 2006, 9:44pm UTC](https://www.ruby-forum.com/t/set-the-foreign-key-constraint-column-name/51582/1 "2006-01-03T21:44:12Z")

</div>

I am applying Rails to an existing schema and not sure how much the  
existing developers will let me go in and rename things. So I need to  
discover the limits of what I can change with Rails.

I’ve found set\_table\_name and set\_primary\_key, which have been very  
useful.

If I have a “foo has\_one :bar” relationship, but then in the bar table  
my column is named “fooid” or some other thing that is not “foo\_id”, is  
there a similar helper command I can use to change it? Everything’s  
working swimmingly for me, except when I get to this bit and it errors  
out telling me “cannot find bar.foo\_id” and I can’t figure out a way to  
point to bar.fooid and say “There it is.”

I tried googling around for “set foreign key constraint column name” but  
can’t seem to track anything down.

Thanks!

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [January 3, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/set-the-foreign-key-constraint-column-name/51582/2 "2006-01-03T21:46:59Z")

</div>

Duane wrote:

> I am applying Rails to an existing schema and not sure how much the  
> existing developers will let me go in and rename things. So I need to  
> discover the limits of what I can change with Rails.
> 
> I’ve found set\_table\_name and set\_primary\_key, which have been very  
> useful.
> 
> If I have a “foo has\_one :bar” relationship, but then in the bar table  
> my column is named “fooid” or some other thing that is not “foo\_id”, is  
> there a similar helper command I can use to change it? Everything’s  
> working swimmingly for me, except when I get to this bit and it errors  
> out telling me “cannot find bar.foo\_id” and I can’t figure out a way to  
> point to bar.fooid and say “There it is.”
> 
> I tried googling around for “set foreign key constraint column name” but  
> can’t seem to track anything down.
> 
> Thanks!

Look for the docs for has\_one  
There is a :foreign\_key option there

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

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [January 3, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/set-the-foreign-key-constraint-column-name/51582/3 "2006-01-03T21:50:37Z")

</div>

Hi, Duane,

the relationships (for example has\_many) take options. One of these  
options is foreign\_key. See  
[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000471](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000471)  
for example.

Regards  
Jan

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [January 4, 2006, 3:04am UTC](https://www.ruby-forum.com/t/set-the-foreign-key-constraint-column-name/51582/4 "2006-01-04T03:04:00Z")

</div>

foo has\_one :bar, :foreign\_key =\> “fooid”

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [January 3, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/set-the-foreign-key-constraint-column-name/51582/5 "2006-01-03T22:38:37Z")

</div>

Jan P. wrote:

> Hi, Duane,
> 
> the relationships (for example has\_many) take options. One of these  
> options is foreign\_key. See  
> [http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000471](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000471)  
> for example.
> 
> Regards  
> Jan

Thanks everybody, sorry for the RTFM. I was going through “Agile Web  
Development with Rails” and glanced right over it because that option  
was discussed as part of belongs\_to, not has\_to. That’s what I get for  
speedskimming.

I knew it had to be an easy answer :).

d
