# How can i create 2 foreign key in a table referring key from one table?

**URL:** <https://www.ruby-forum.com/t/how-can-i-create-2-foreign-key-in-a-table-referring-key-from-one-table/117726>\
**Category:** Rails\
**Created:** [October 19, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/how-can-i-create-2-foreign-key-in-a-table-referring-key-from-one-table/117726 "2007-10-19T19:02:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amjith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c0e974/32.png) [@Amjith](https://www.ruby-forum.com/u/Amjith)\
**Post date:** [October 19, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/how-can-i-create-2-foreign-key-in-a-table-referring-key-from-one-table/117726/1 "2007-10-19T19:02:30Z")

</div>

hi Friends,  
i have a accounts table and a transaction table see the schema below

1. 

```
  acc_master

```

`acc_id` int(11) – primary key  
`acc_name`  
`cur_balance`  
`regist_date`  
`update_date`

2. 

```
 acc_trans

```

`trans_id` – primary key  
`trans_from` – foreign key reference to acc\_id (acc\_master)  
`trans_to` — foreign key reference to acc\_id (acc\_master)  
`trans_date`  
`trans_amount`  
`trans_narration`

How can i create a migration file for the 2nd table … how will i  
represent 2 foreign key from same table in Rails?  
Can any one help for this question please?

Thanks  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Amjith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c0e974/32.png) [@Amjith](https://www.ruby-forum.com/u/Amjith)\
**Post date:** [October 19, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/how-can-i-create-2-foreign-key-in-a-table-referring-key-from-one-table/117726/2 "2007-10-19T19:20:37Z")

</div>

On 19 Oct 2007, at 18:01, Amjith wrote:

> How can i create a migration file for the 2nd table … how will i  
> represent 2 foreign key from same table in Rails?  
> Can any one help for this question please?

belongs\_to :foo\_1, :class\_name =\> ‘Foo’, :foreign\_key =\> ‘foo\_1\_id’  
belongs\_to :foo\_2, :class\_name =\> ‘Foo’, :foreign\_key =\> ‘foo\_2\_id’

Fred

>

---

<div class="post-metadata">

**Author:** ![Amjith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c0e974/32.png) [@Amjith](https://www.ruby-forum.com/u/Amjith)\
**Post date:** [March 31, 2008, 8:24am UTC](https://www.ruby-forum.com/t/how-can-i-create-2-foreign-key-in-a-table-referring-key-from-one-table/117726/3 "2008-03-31T08:24:28Z")

</div>

Frederick C. wrote:

> On 19 Oct 2007, at 18:01, Amjith wrote:
> 
> > How can i create a migration file for the 2nd table … how will i  
> > represent 2 foreign key from same table in Rails?  
> > Can any one help for this question please?
> 
> belongs\_to :foo\_1, :class\_name =\> ‘Foo’, :foreign\_key =\> ‘foo\_1\_id’  
> belongs\_to :foo\_2, :class\_name =\> ‘Foo’, :foreign\_key =\> ‘foo\_2\_id’
> 
> Fred
> 
> >

Thankyou verymuch Fred
