# Foreign key plugin

**URL:** <https://www.ruby-forum.com/t/foreign-key-plugin/84847>\
**Category:** Rails\
**Created:** [February 8, 2007, 2:13am UTC](https://www.ruby-forum.com/t/foreign-key-plugin/84847 "2007-02-08T02:13:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MSBarbosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7c8e57/32.png) [@MSBarbosa](https://www.ruby-forum.com/u/MSBarbosa)\
**Post date:** [February 8, 2007, 2:13am UTC](https://www.ruby-forum.com/t/foreign-key-plugin/84847/1 "2007-02-08T02:13:09Z")

</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

– create\_table(:interests)  
rake aborted!  
Mysql::Error: #HY000Can’t create table ‘./qinqy\_development/  
interests.frm’ (errno: 150): CREATE TABLE interests (`id` int(11)  
DEFAULT NULL auto\_increment PRIMARY KEY, `location_id` int(11) NOT  
NULL, `interest_type_id` int(11) NOT NULL, FOREIGN KEY (location\_id)  
REFERENCES locations (id), FOREIGN KEY (interest\_type\_id) REFERENCES  
interest\_types (id)) ENGINE=InnoDB

The migration code for the interests table is as follows:

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

def self.down  
drop\_table :interests  
end  
end

So there are 2 foreign keys in this interests table, referring to  
location\_id and interest\_type\_id. According to the foreign key  
migrations plugin, this code should be working. However, I keep  
getting this error which prevents the migrations to take place. What  
am I doing wrong?

---

<div class="post-metadata">

**Author:** ![MSBarbosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7c8e57/32.png) [@MSBarbosa](https://www.ruby-forum.com/u/MSBarbosa)\
**Post date:** [February 8, 2007, 5:52am UTC](https://www.ruby-forum.com/t/foreign-key-plugin/84847/2 "2007-02-08T05:52:22Z")

</div>

On 2/7/07, M. Barbosa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =================================================  
> The migration code for the interests table is as follows:  
> drop\_table :interests  
> end  
> end
> 
> So there are 2 foreign keys in this interests table, referring to  
> location\_id and interest\_type\_id. According to the foreign key  
> migrations plugin, this code should be working. However, I keep  
> getting this error which prevents the migrations to take place. What  
> am I doing wrong?

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

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![MSBarbosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7c8e57/32.png) [@MSBarbosa](https://www.ruby-forum.com/u/MSBarbosa)\
**Post date:** [February 8, 2007, 1:12pm UTC](https://www.ruby-forum.com/t/foreign-key-plugin/84847/3 "2007-02-08T13:12:08Z")

</div>

On Feb 8, 2007, at 1:04 PM, Michel B wrote:

> 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?

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:** ![MSBarbosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7c8e57/32.png) [@MSBarbosa](https://www.ruby-forum.com/u/MSBarbosa)\
**Post date:** [February 8, 2007, 1:05pm UTC](https://www.ruby-forum.com/t/foreign-key-plugin/84847/4 "2007-02-08T13:05:06Z")

</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:** ![MSBarbosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7c8e57/32.png) [@MSBarbosa](https://www.ruby-forum.com/u/MSBarbosa)\
**Post date:** [February 8, 2007, 1:20pm UTC](https://www.ruby-forum.com/t/foreign-key-plugin/84847/5 "2007-02-08T13:20:11Z")

</div>

On Feb 8, 2007, at 1:14 PM, Michel B wrote:

> def self.down  
> drop\_table :interests  
> end  
> end

_Before_ that migration runs, tables “locations” and “interest\_types”  
must have been created, possibly in earlier migrations.

– fxn

---

<div class="post-metadata">

**Author:** ![MSBarbosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7c8e57/32.png) [@MSBarbosa](https://www.ruby-forum.com/u/MSBarbosa)\
**Post date:** [February 8, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/foreign-key-plugin/84847/6 "2007-02-08T13:15:08Z")

</div>

The migration for interest\_types looks like this:

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

def self.down  
drop\_table :interests  
end  
end

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

# == CreateInterests: migrating

– create\_table(:interests)  
rake aborted!  
Mysql::Error: #HY000Can’t create table ‘./qinqy\_development/  
interests.frm’ (errno: 150): CREATE TABLE interests (`id` int(11)  
DEFAULT NULL auto\_increment PRIMARY KEY, `location_id` int(11) NOT  
NULL, `interest_type_id` int(11) NOT NULL, FOREIGN KEY (location\_id)  
REFERENCES locations (id), FOREIGN KEY (interest\_type\_id) REFERENCES  
interest\_types (id)) ENGINE=InnoDB

How do I use this foreign key plugin? Do I have to use it in this way?

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

def self.down  
drop\_table :interests  
end  
end

Now there are no foreign keys, but the table will be created  
successfully. How do I go about creating the foreign key after this?
