# foreign key not working

**URL:** <https://discuss.rubyonrails.org/t/foreign-key-not-working/11055>\
**Category:** rubyonrails-talk\
**Created:** [March 31, 2007, 12:13am UTC](https://discuss.rubyonrails.org/t/foreign-key-not-working/11055 "2007-03-31T00:13:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luma](https://avatars.discourse-cdn.com/v4/letter/l/d6d6ee/32.png) [@Luma](https://discuss.rubyonrails.org/u/Luma)\
**Post date:** [March 31, 2007, 12:13am UTC](https://discuss.rubyonrails.org/t/foreign-key-not-working/11055/1 "2007-03-31T00:13:16Z")

</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 &nbsp;&nbsp;def self.up &nbsp;&nbsp;&nbsp;&nbsp;execute "alter table my\_items &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add constraint fk\_myid &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreign key (myid) references products(myid)" &nbsp;&nbsp;end

&nbsp;&nbsp;def self.down &nbsp;&nbsp;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
