# Relationships between tables issue

**URL:** <https://www.ruby-forum.com/t/relationships-between-tables-issue/125184>\
**Category:** Rails\
**Created:** [January 6, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/relationships-between-tables-issue/125184 "2008-01-06T20:11:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![myst\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@myst\_tt](https://www.ruby-forum.com/u/myst_tt)\
**Post date:** [January 6, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/relationships-between-tables-issue/125184/1 "2008-01-06T20:11:26Z")

</div>

Hi, there:  
I made two models - “task” and “comment” in my app without any  
connections each other, but now I wanna a “one to many” between them,  
saying one task has many comments, one comments has to belong to one  
task.  
Therefore, I add “belongs\_to :task” and “has\_many :comments” in each  
model respectively.

The question is:  
1\> I dont think the two tables in the database will add the  
foreign\_key column inside automatically after I said “belongs\_to &  
has” in models. Am I right? At least I didnt find it by investigating  
database tables.  
2\> what should I do if I dont wanna use SQL or DDL to create  
foreign\_key cloumn, is the “migrate” able to help me?

Thanks a lot!  
Myst

---

<div class="post-metadata">

**Author:** ![myst\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@myst\_tt](https://www.ruby-forum.com/u/myst_tt)\
**Post date:** [January 6, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/relationships-between-tables-issue/125184/2 "2008-01-06T22:21:00Z")

</div>

myst\_tt wrote:

> Hi, there:  
> I made two models - “task” and “comment” in my app without any  
> connections each other, but now I wanna a “one to many” between them,  
> saying one task has many comments, one comments has to belong to one  
> task.  
> Therefore, I add “belongs\_to :task” and “has\_many :comments” in each  
> model respectively.

thats good and how it should be done

> The question is:  
> 1\> I dont think the two tables in the database will add the  
> foreign\_key column inside automatically after I said “belongs\_to &  
> has” in models. Am I right? At least I didnt find it by investigating  
> database tables.

you need to add the foreign keys yourself. rails is not doing it for  
you. if you stick to the convention that the foreign key is named  
id\_classname then it will be picked up by the relationshsip  
automatically … otherwise you will need to specify it explicitely in  
the code

> 2\> what should I do if I dont wanna use SQL or DDL to create  
> foreign\_key cloumn, is the “migrate” able to help me?

yep, use a migration to add the column you need

> Thanks a lot!  
> Myst

---

<div class="post-metadata">

**Author:** ![myst\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@myst\_tt](https://www.ruby-forum.com/u/myst_tt)\
**Post date:** [January 6, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/relationships-between-tables-issue/125184/3 "2008-01-06T23:47:15Z")

</div>

Thank you!

On 1ÔÂ6ÈÕ, ÏÂÎç11Ê±21·Ö, Michal G. \<rails-mailing-l…@andreas-
