# Foreign key plugin

**URL:** <https://discuss.rubyonrails.org/t/foreign-key-plugin/8385>\
**Category:** rubyonrails-talk\
**Created:** [February 8, 2007, 1:12am UTC](https://discuss.rubyonrails.org/t/foreign-key-plugin/8385 "2007-02-08T01:12:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_Barbosa](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@M\_Barbosa](https://discuss.rubyonrails.org/u/M_Barbosa)\
**Post date:** [February 8, 2007, 1:12am UTC](https://discuss.rubyonrails.org/t/foreign-key-plugin/8385/1 "2007-02-08T01:12:37Z")

</div>

Hi guys, i have just installed the foreign key pluging from http:// www.redhillconsulting.com.au/ rails\_plugins.html#foreign\_key\_migrations.

The problem that I'm facing is the following: I have set up migrations for 7 tables in a MySQL database. However, when I try to execute rake db:migrate i get the following error:

== CreateInterests: migrating

---

<div class="post-metadata">

**Author:** ![Zack\_Chandler](https://avatars.discourse-cdn.com/v4/letter/z/a698b9/32.png) [@Zack\_Chandler](https://discuss.rubyonrails.org/u/Zack_Chandler)\
**Post date:** [February 8, 2007, 4:51am UTC](https://discuss.rubyonrails.org/t/foreign-key-plugin/8385/2 "2007-02-08T04:51:19Z")

</div>

Are you sure there exists in the DB a locations table with an id field and an interest\_types table with an id column?

---

<div class="post-metadata">

**Author:** ![M\_Barbosa](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@M\_Barbosa](https://discuss.rubyonrails.org/u/M_Barbosa)\
**Post date:** [February 8, 2007, 12:04pm UTC](https://discuss.rubyonrails.org/t/foreign-key-plugin/8385/3 "2007-02-08T12:04:30Z")

</div>

The locations and interest\_types migration files don't explicitly define the id field. If I'm not mistaken, you don't have to specifcy the id field. Or will the problem be solved if the interest\_types en locations table ALREADY exist, before I apply any foreign keys?

---

<div class="post-metadata">

**Author:** ![fxn](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fxn/32/12956_2.png) [@fxn](https://discuss.rubyonrails.org/u/fxn)\
**Post date:** [February 8, 2007, 12:07pm UTC](https://discuss.rubyonrails.org/t/foreign-key-plugin/8385/4 "2007-02-08T12:07:11Z")

</div>

Yes. Its create\_table must come before, because the constraint needs to refer to some column known by the database.

When you use that plugin the order of (create|drop)\_table matters.

-- fxn

---

<div class="post-metadata">

**Author:** ![M\_Barbosa](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@M\_Barbosa](https://discuss.rubyonrails.org/u/M_Barbosa)\
**Post date:** [February 8, 2007, 12:14pm UTC](https://discuss.rubyonrails.org/t/foreign-key-plugin/8385/5 "2007-02-08T12:14:14Z")

</div>

The migration for interest\_types looks like this:

class CreateInterests \< ActiveRecord::Migration &nbsp;&nbsp;def self.up &nbsp;&nbsp;&nbsp;&nbsp;create\_table :interests do |t| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :location\_id, :integer, :null =\> false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :interest\_type\_id, :integer, :null =\> false &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;def self.down &nbsp;&nbsp;&nbsp;&nbsp;drop\_table :interests &nbsp;&nbsp;end end

I have deleted the other 6 migrations. If I perform rake db:migrate I still get this error:

== CreateInterests: migrating

---

<div class="post-metadata">

**Author:** ![fxn](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fxn/32/12956_2.png) [@fxn](https://discuss.rubyonrails.org/u/fxn)\
**Post date:** [February 8, 2007, 12:19pm UTC](https://discuss.rubyonrails.org/t/foreign-key-plugin/8385/6 "2007-02-08T12:19:12Z")

</div>

\*Before\* that migration runs, tables "locations" and "interest\_types" must have been created, possibly in earlier migrations.

-- fxn
