# Foreign key not working

**URL:** <https://www.ruby-forum.com/t/foreign-key-not-working/90697>\
**Category:** Rails\
**Created:** [March 31, 2007, 2:14am UTC](https://www.ruby-forum.com/t/foreign-key-not-working/90697 "2007-03-31T02:14:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![raven13](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@raven13](https://www.ruby-forum.com/u/raven13)\
**Post date:** [March 31, 2007, 2:14am UTC](https://www.ruby-forum.com/t/foreign-key-not-working/90697/1 "2007-03-31T02:14:25Z")

</div>

Hello,

I’m using InstantRails 1.6 on Windows XP with Mysql 5.0.27, Rails  
1.2.2, and Ruby 1.8.5.

The following foreign key is not working.

class FkLibriid \< ActiveRecord::Migration  
def self.up  
execute “alter table my\_items  
add constraint fk\_myid  
foreign key (myid) references products(myid)”  
end

def self.down  
end  
end

I declared myid in both tables the same:  
t.column :myid, :integer, :null =\> false

And myid is not primary key in any table. I don’t like it to be one.

Additionally, I’ve set the following in the model for the referenced  
table:  
validates\_uniqueness\_of :myid

# C:\Myshop\shop\>rake db:migrate --trace (in C:/Myshop/shop) \*\* Invoke db:migrate (first\_time) \*\* Invoke environment (first\_time) \*\* Execute environment \*\* Execute db:migrate == FkLibriid: migrating

– execute(“alter table my\_items\n add constraint fk\_myid  
\n  
foreign key (myid) references products(myid)”)  
rake aborted!  
Mysql::Error: #HY000Can’t create table ‘.\shop\_development  
#sql-464\_23.frm’ (err  
no: 150): alter table my\_items  
add constraint fk\_myid  
foreign key (myid) references products(myid)

It would be nice if someone could help me.
