Forum: Ruby on Rails foreign key not working

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
70dc5b6a5bf97779ee34d2865539f50b?d=identicon&s=25 Martin Luy (luma)
on 2007-03-31 02:14
(Received via mailing list)

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)"

  def self.down

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
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
         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.
This topic is locked and can not be replied to.