# :foreign\_key isnt work

**URL:** <https://www.ruby-forum.com/t/foreign-key-isnt-work/136414>\
**Category:** Rails\
**Created:** [May 1, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/foreign-key-isnt-work/136414 "2008-05-01T21:45:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![malioboro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@malioboro](https://www.ruby-forum.com/u/malioboro)\
**Post date:** [May 1, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/foreign-key-isnt-work/136414/1 "2008-05-01T21:45:21Z")

</div>

I have hotels, cars and invoices

hotels has attribute invoice\_no in table  
cars has attribute invoice\_no in table

I create like this :

class Hotel \< ActiveRecord  
belongs\_to :invoice, :foreign\_key =\> ‘invoice\_no’

and

class Invoice \< ActiveRecord  
has\_many :hotels, foreign\_key =\> ‘invoice\_no’

example i have invoice\_no = ‘123456’ and it is in both hotel and invoice  
tables.  
After that i test in console :

> > Invoice.find\_by\_invoice\_no(123456).hotels  
> > []  
> > Hotel.find\_by\_invoice\_no(123456).invoice  
> > nil

how come foreign\_key in AR can’t detect it? I check to table in mysql  
invoice\_no 123456 is existent in table hotels and invoices. Any advice ?

Thank you for your suggestion,

Reinhart

---

<div class="post-metadata">

**Author:** ![malioboro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@malioboro](https://www.ruby-forum.com/u/malioboro)\
**Post date:** [May 1, 2008, 9:46pm UTC](https://www.ruby-forum.com/t/foreign-key-isnt-work/136414/2 "2008-05-01T21:46:50Z")

</div>

and invoices has attributes : id, invoice\_no, customer\_id, amount,  
created\_at

---

<div class="post-metadata">

**Author:** ![malioboro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@malioboro](https://www.ruby-forum.com/u/malioboro)\
**Post date:** [May 1, 2008, 10:33pm UTC](https://www.ruby-forum.com/t/foreign-key-isnt-work/136414/3 "2008-05-01T22:33:11Z")

</div>

do you need to setup some type of reverse foreign key [?]

On Thu, May 1, 2008 at 1:46 PM, Visit Indonesia 2008

---

<div class="post-metadata">

**Author:** ![malioboro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@malioboro](https://www.ruby-forum.com/u/malioboro)\
**Post date:** [May 1, 2008, 11:16pm UTC](https://www.ruby-forum.com/t/foreign-key-isnt-work/136414/4 "2008-05-01T23:16:10Z")

</div>

On 1 May 2008, at 20:45, Visit Indonesia 2008 wrote:

> 

Rails takes this to mean 'the invoice\_no column on hotel references  
the primary key of the invoices table

> and
> 
> class Invoice \< ActiveRecord  
> has\_many :hotels, foreign\_key =\> ‘invoice\_no’

Same here. Rails associations always from a foreign key column to a  
primary key.

Fred

---

<div class="post-metadata">

**Author:** ![malioboro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@malioboro](https://www.ruby-forum.com/u/malioboro)\
**Post date:** [May 2, 2008, 2:15am UTC](https://www.ruby-forum.com/t/foreign-key-isnt-work/136414/5 "2008-05-02T02:15:53Z")

</div>

Why are you storing invoice\_no in the Hotels table? Are you creating a  
new row for every invoice? Or is that an invoice\_id? Like a hotel\_id?

As I see it, invoices is basically a join table linking hotels to cars  
so you need to set up a has\_and\_belong\_to\_many relationship between  
hotels and cars through the invoices table. You shouldn’t be storing  
invoice data in the Hotel model. That don’t make no sense…

ESPNDev

---

<div class="post-metadata">

**Author:** ![malioboro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@malioboro](https://www.ruby-forum.com/u/malioboro)\
**Post date:** [May 2, 2008, 4:32am UTC](https://www.ruby-forum.com/t/foreign-key-isnt-work/136414/6 "2008-05-02T04:32:16Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Why are you storing invoice\_no in the Hotels table? Are you creating a  
> new row for every invoice? Or is that an invoice\_id? Like a hotel\_id?
> 
> As I see it, invoices is basically a join table linking hotels to cars  
> so you need to set up a has\_and\_belong\_to\_many relationship between  
> hotels and cars through the invoices table. You shouldn’t be storing  
> invoice data in the Hotel model. That don’t make no sense…
> 
> ESPNDev

This database is not created by me, I have job to develop/maintain  
their application. So that database was composed by previous programmer.

IN table invoices has no hotel\_id and car\_id, only has :  
id, invoice\_no, customer\_id, amount, created\_at

hotels table has id, invoice\_no, price, amount. And  
cars table has id, invoice\_no, price, amount.

It is impossible if i have to change invoice\_no to be invoice\_id,  
because there are already queries / records in those tables.

Please give me suggestion to make it works properly as foreign key using  
invoice\_no.

Thanks

Reinhart

---

<div class="post-metadata">

**Author:** ![malioboro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@malioboro](https://www.ruby-forum.com/u/malioboro)\
**Post date:** [May 2, 2008, 4:26am UTC](https://www.ruby-forum.com/t/foreign-key-isnt-work/136414/7 "2008-05-02T04:26:03Z")

</div>

Frederick C. wrote:

> On 1 May 2008, at 20:45, Visit Indonesia 2008 wrote:
> 
> > 
> 
> Rails takes this to mean 'the invoice\_no column on hotel references  
> the primary key of the invoices table
> 
> > and
> > 
> > class Invoice \< ActiveRecord  
> > has\_many :hotels, foreign\_key =\> ‘invoice\_no’
> 
> Same here. Rails associations always from a foreign key column to a  
> primary key.
> 
> Fred

What is the solution Fred? Give me a clue.

Thank you bro,

Reinhart
