# Ruby on Rails Database with foreign keys?

**URL:** <https://www.ruby-forum.com/t/ruby-on-rails-database-with-foreign-keys/90097>\
**Category:** Rails\
**Created:** [March 26, 2007, 2:54am UTC](https://www.ruby-forum.com/t/ruby-on-rails-database-with-foreign-keys/90097 "2007-03-26T02:54:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 26, 2007, 2:54am UTC](https://www.ruby-forum.com/t/ruby-on-rails-database-with-foreign-keys/90097/1 "2007-03-26T02:54:37Z")

</div>

Hi,

I started a project on Ruby on Rails some time ago when the migration  
feature wasn’t part of Rails. I used to have one big SQL file that  
creates the whole database and adds the foreign constraints. They  
looked like that:

CONSTRAINT FK\_users\_countries  
FOREIGN KEY FK\_users\_countries (country\_id)  
REFERENCES countries(country\_id)  
ON DELETE RESTRICT  
ON UPDATE RESTRICT

Now I want to use migration features so I used

rake db:schema:dump

When I look at the schema.rb file i don’t like what i see ;(. All the  
tables are there but the are no primary keys and also there are no  
Foreign Keys.

My question is can i use the ruby way to create foreign keys? And why

rake db:schema:dump

doesn’t work?

Nikola

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 26, 2007, 4:26am UTC](https://www.ruby-forum.com/t/ruby-on-rails-database-with-foreign-keys/90097/2 "2007-03-26T04:26:43Z")

</div>

Rails nerds are not big on FKs - I don’t like it but if want to play  
along - learn to.

You can put FK constraints in your migrations as SQL statements -  
execute “”" ALTER … “”"

The FK don’t show up in the schema.rb - See first statement above

They cause problems with fixtures - See first statement above

On Mar 25, 2:53 pm, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 26, 2007, 5:15am UTC](https://www.ruby-forum.com/t/ruby-on-rails-database-with-foreign-keys/90097/3 "2007-03-26T05:15:45Z")

</div>

google for the foreign key migrations plugin from redhill consulting. I  
use  
that on my rails apps and it works great.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 26, 2007, 5:19am UTC](https://www.ruby-forum.com/t/ruby-on-rails-database-with-foreign-keys/90097/4 "2007-03-26T05:19:46Z")

</div>

There is a plugin, from RedHill consulting, that allow you create FKÂ´s:  
[http://www.redhillconsulting.com.au/rails\_plugins.html](http://www.redhillconsulting.com.au/rails_plugins.html)

2007/3/25, dysinger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> They cause problems with fixtures - See first statement above
> 
> > CONSTRAINT FK\_users\_countries  
> > tables are there but the are no primary keys and also there are no
> 
> >

–  
Weverton G. de Morais  
TecnÃ³logo em Redes de ComunicaÃ§Ã£o  
Desenvolvedor Delphi
