# Are references and Foreign keys the same thing?

**URL:** <https://www.ruby-forum.com/t/are-references-and-foreign-keys-the-same-thing/150679>\
**Category:** Rails\
**Created:** [October 11, 2008, 11:53am UTC](https://www.ruby-forum.com/t/are-references-and-foreign-keys-the-same-thing/150679 "2008-10-11T11:53:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [October 11, 2008, 11:53am UTC](https://www.ruby-forum.com/t/are-references-and-foreign-keys-the-same-thing/150679/1 "2008-10-11T11:53:19Z")

</div>

Hi,

I’m getting confused, are references and foreign keys the same thing  
really?

> **[【手机钱包app下载】投资基金千万不可跟风，有可能你就是下一个接盘侠-tp冷钱包下载](https://www.jonathansng.com/ruby-on-rails/foreign-key-migrations-in-rails-20/)**
>
> 之前买基金时有多高兴，如今就有多忧伤。不怨大伙儿心态崩了。2022年至今，基金亏本已成为一种习惯，即使是大牌明星基金经理，绝大部分的亏本力度还在20%-40%中间。她们跌下了圣坛，偶尔还会便被拉出“遛

Is there a good example on FKs?  
I’d like to see how it goes in practice, for example how deleting the  
parent record would delete any existing records depending on it, and  
also how to avoid that from occurring…

Cheers.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [October 11, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/are-references-and-foreign-keys-the-same-thing/150679/2 "2008-10-11T12:19:25Z")

</div>

On Oct 11, 10:53 am, Pod C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,
> 
> I’m getting confused, are references and foreign keys the same thing  
> really?

Sort of. I suspect what you are actually wondering about are foreign  
key constraints (where the database enforces that each foo\_id column  
does indeed contain the id of a row from the foos table.  
Rails doesn’t provide anything builtin for doing this, although there  
are various plugins.  
The documentation for your database should provide information on how  
it handles foreign keys  
Fred

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [October 11, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/are-references-and-foreign-keys-the-same-thing/150679/3 "2008-10-11T12:32:37Z")

</div>

Hi, thanks Fred.

Yes you are right I’m after FK constraints. And I guess that a way to do  
that is for example using this in a migration file:  
execute “alter table user\_images add constraint fk\_wi\_user foreign key  
(user\_id) references users(id)”

Regards

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [October 11, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/are-references-and-foreign-keys-the-same-thing/150679/4 "2008-10-11T22:22:29Z")

</div>

Hi,

Yeah now I checked about foreign key constraints for MySql:  
[http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html](http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html)

I think I understand it a bit better now.  
Using references or something like t.integer :project\_id help creating  
the relations between tables, and help with queries like SELECT \* FROM  
projects WHERE user\_id = 22;

The foreign key constrains help using those keys to keep DB data  
consistent for example. As I mentioned I was actually looking for ON  
DELETE CASCADE so that was missing on my constraint definition above. It  
should be:

execute “alter table tasks add constraint fk\_projects foreign key  
(project\_id) references projects(id) ON DELETE CASCADE”

* * *

Writing it here to put my thought in order…  
Please correct me if I’m still mixing up things.

Regards

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [October 13, 2008, 3:46am UTC](https://www.ruby-forum.com/t/are-references-and-foreign-keys-the-same-thing/150679/5 "2008-10-13T03:46:54Z")

</div>

To make this easier to do, you might consider writing yourself a little  
helper, such as

def foreign\_key(foreign\_table, foreign\_column, primary\_table,  
primary\_column = :id)  
execute "  
alter table #{foreign\_table.to\_s}  
add constraint fk\_#{foreign\_table.to\_s}\_#{foreign\_column.to\_s}  
foreign key (#{foreign\_column.to\_s}) references  
#{primary\_table.to\_s} (#{primary\_column.to\_s})  
"  
end

def delete\_foreign\_key(foreign\_table, foreign\_column)  
execute “alter table #{foreign\_table.to\_s} drop constraint  
fk\_#{foreign\_table.to\_s}\_#{foreign\_column.to\_s}”  
end

so you can use it like

foreign\_key :addresses, :person\_id, :people  
delete\_foreign\_key :addresses, :person\_id

Peace.
