# Migrations + foreign keys

**URL:** <https://www.ruby-forum.com/t/migrations-foreign-keys/98269>\
**Category:** Rails\
**Created:** [July 13, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/migrations-foreign-keys/98269 "2006-07-13T16:47:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [July 13, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/migrations-foreign-keys/98269/1 "2006-07-13T16:47:41Z")

</div>

Hi all,

In the AWDR author use the execute method to declare foreign keys:

execute “alter table… add constraint… foreign key…”

Is there a ruby sintax to declare a foreign key in a migration class?

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [July 13, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/migrations-foreign-keys/98269/2 "2006-07-13T17:04:05Z")

</div>

On 7/13/06, Eduardo Yáñez Parareda [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> In the AWDR author use the execute method to declare foreign keys:
> 
> execute “alter table… add constraint… foreign key…”
> 
> Is there a ruby sintax to declare a foreign key in a migration class?

I’ve not used it, but there is a foreign key migrations plugin.

[http://www.redhillconsulting.com.au/rails\_plugins.html#foreign\_key\_migrations](http://www.redhillconsulting.com.au/rails_plugins.html#foreign_key_migrations)

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [July 13, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/migrations-foreign-keys/98269/3 "2006-07-13T17:05:55Z")

</div>

> [http://www.redhillconsulting.com.au/rails\_plugins.html#foreign\_key\_migrations](http://www.redhillconsulting.com.au/rails_plugins.html#foreign_key_migrations)

Thanks, I’m going to have a quick look at this

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [July 14, 2006, 4:48pm UTC](https://www.ruby-forum.com/t/migrations-foreign-keys/98269/4 "2006-07-14T16:48:45Z")

</div>

I didn’t realize the solution proposed in the book. Create a new module  
with  
a foreign\_key method which invokes execute alter table, and include that  
module  
in the migration’s classes that need it.

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [July 14, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/migrations-foreign-keys/98269/5 "2006-07-14T18:01:06Z")

</div>

On 7/14/06, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Which book?

2nd edition of Agile Web D. With Rails. Page 268.

– James

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [July 14, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/migrations-foreign-keys/98269/6 "2006-07-14T17:06:52Z")

</div>

Which book?

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [July 14, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/migrations-foreign-keys/98269/7 "2006-07-14T18:21:44Z")

</div>

I had problems with the foreign\_key\_migrations plugin. Is was possible  
to  
generate a contraint that was not removable via MySQL ddl. Only  
solution  
was to drop and re-create the database. Happened twice. ( In all  
fairness, I  
haven’t researched the problem, and haven’t contacted the autors about  
it).

I am using the foreign\_key\_schema\_dumper plugin in my current project.  
It  
provides to migration methods for managing foreign keys in migrations.

add\_foreign\_key\_constraint  
and  
remove\_foreign\_key\_constraint

You might want to look to see how they are implements, or just use them  
if  
they do what you want.  
my 2 cents  
-Larry
