# Another migration problem

**URL:** <https://www.ruby-forum.com/t/another-migration-problem/68647>\
**Category:** Rails\
**Created:** [September 14, 2006, 5:05am UTC](https://www.ruby-forum.com/t/another-migration-problem/68647 "2006-09-14T05:05:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hotgazpacho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@hotgazpacho](https://www.ruby-forum.com/u/hotgazpacho)\
**Post date:** [September 14, 2006, 5:05am UTC](https://www.ruby-forum.com/t/another-migration-problem/68647/1 "2006-09-14T05:05:56Z")

</div>

I’ve added the following line to a migration file after all the  
create\_table statements. For the life of me, I can’t figure out what is  
wrong with it.

execute “ALTER TABLE users\_roles ADD CONSTRAINT FK\_UserRoles\_Users  
FOREIGN KEY(user\_id) REFERENCES users(user\_id)”

Here’s the error I’m getting.

execute(“ALTER TABLE users\_roles ADD CONSTRAINT FK\_UserRoles\_Users  
FOREIGN KEY(user\_id) REFERENCES users(user\_id)”)  
rake aborted!  
Mysql::Error: Can’t create table ‘./HoshinWeb\_dev/#sql-118c\_c.frm’  
(errno: 150): ALTER TABLE users\_roles ADD CONSTRAINT FK\_UserRoles\_Users  
FOREIGN KEY(user\_id) REFERENCES users(user\_id)

The code looks right, but it’s late and I’m sleep-deprived. What am I  
doing wrong? Why in the world is it trying to create a table here?

* * *

How low will we go? Check out Yahoo! MessengerÂ?s low PC-to-Phone call  
rates.

---

<div class="post-metadata">

**Author:** ![hotgazpacho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@hotgazpacho](https://www.ruby-forum.com/u/hotgazpacho)\
**Post date:** [September 14, 2006, 7:26am UTC](https://www.ruby-forum.com/t/another-migration-problem/68647/2 "2006-09-14T07:26:07Z")

</div>

Will,

This is not a migration issue but rather what appears to be something  
specific with MySQL - Rails isn’t trying to create any table - it’s on  
the  
MySQL side. Google “errno: 150” and have a look at the possible  
problems.  
There seems to be a pile of things that can pop this error. The second  
hit  
on google appears to use show innodb status to get more information out  
of  
the system (you would need to attempt the ALTER TABLE manually and you  
would  
obviously need to be using innodb).

Sorry can’t be more specific but this appears to be a catch all error  
that  
don’t reveal much about why it occurs…other then Rails isn’t the  
culprit  
🙂

John W Higgins  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![hotgazpacho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@hotgazpacho](https://www.ruby-forum.com/u/hotgazpacho)\
**Post date:** [September 14, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/another-migration-problem/68647/3 "2006-09-14T18:15:22Z")

</div>

Doh! It was the quotes. I happened to have an old todo list sample on my  
system, so I added a categories table and a field in the todo items  
table that pointed at it. MySQL Admin generated the following code:

ALTER TABLE `todo`.`todos` ADD CONSTRAINT `FK_todos_categories` FOREIGN  
KEY `FK_todos_categories` (`categoryid`)  
REFERENCES `todocategories` (`id`)  
ON DELETE RESTRICT  
ON UPDATE RESTRICT;

It started making sense as soon as I’d had my morning coffee.

John H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: Will,

This is not a migration issue but rather what appears to be something  
specific with MySQL - Rails isn’t trying to create any table - it’s on  
the MySQL side. Google “errno: 150” and have a look at the possible  
problems. There seems to be a pile of things that can pop this error.  
The second hit on google appears to use show innodb status to get more  
information out of the system (you would need to attempt the ALTER TABLE  
manually and you would obviously need to be using innodb).

Sorry can’t be more specific but this appears to be a catch all error  
that don’t reveal much about why it occurs…other then Rails isn’t the  
culprit 🙂

John W Higgins  
removed\_email\_address@domain.invalid

On 9/13/06, Will G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: I’ve added the  
following line to a migration file after all the create\_table  
statements. For the life of me, I can’t figure out what is wrong with  
it.

execute “ALTER TABLE users\_roles ADD CONSTRAINT FK\_UserRoles\_Users  
FOREIGN KEY(user\_id) REFERENCES users(user\_id)”

Here’s the error I’m getting.

execute(“ALTER TABLE users\_roles ADD CONSTRAINT FK\_UserRoles\_Users  
FOREIGN KEY(user\_id) REFERENCES users(user\_id)”)  
rake aborted!  
Mysql::Error: Can’t create table ‘./HoshinWeb\_dev/#sql-118c\_c.frm’  
(errno: 150): ALTER TABLE users\_roles ADD CONSTRAINT FK\_UserRoles\_Users  
FOREIGN KEY(user\_id) REFERENCES users(user\_id)

The code looks right, but it’s late and I’m sleep-deprived. What am I  
doing wrong? Why in the world is it trying to create a table here?

* * *

How low will we go? Check out Yahoo! Messenger’s low PC-to-Phone call  
rates.

* * *

Get your email and more, right on the new [Yahoo.com](http://Yahoo.com)

---

<div class="post-metadata">

**Author:** ![hotgazpacho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@hotgazpacho](https://www.ruby-forum.com/u/hotgazpacho)\
**Post date:** [September 15, 2006, 1:16am UTC](https://www.ruby-forum.com/t/another-migration-problem/68647/4 "2006-09-15T01:16:48Z")

</div>

Nevermind. It wasn’t just the quotes. I followed the recommendations on  
this page:

[http://seb.box.re/articles/2006/07/29/foreign-key-and-rails-migration#comments](http://seb.box.re/articles/2006/07/29/foreign-key-and-rails-migration#comments)

Then, I added the migration\_helpers.rb file under my lib directory in my  
site. I changed the foreign key calls to look like:  
foreign\_key(:users\_roles, :userid, :users)

However, I get the following complaint when I run rake:  
rake aborted!  
undefined method `foreign\_key’ for  
#ActiveRecord::ConnectionAdapters::MysqlAdapter:0xb75a5d0c

The declaration in my migration file looks like this:  
require “migration\_helpers”  
class MyWeb \< ActiveRecord::Migration  
extend MigrationHelpers  
def self.up

Any ideas?

One more thing. When this thing runs and breaks in the middle, it’s a  
real pain having to go back and clear out the table. Is there some sort  
of trick I can use to wrap this in a transaction or something?

Will G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: Doh! It was the quotes. I  
happened to have an old todo list sample on my system, so I added a  
categories table and a field in the todo items table that pointed at it.  
MySQL Admin generated the following code:

ALTER TABLE `todo`.`todos` ADD CONSTRAINT `FK_todos_categories` FOREIGN  
KEY `FK_todos_categories` (`categoryid`)  
REFERENCES `todocategories` (`id`)  
ON DELETE RESTRICT  
ON UPDATE RESTRICT;

It started making sense as soon as I’d had my morning coffee.

John H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: Will,

This is not a migration issue but rather what appears to be something  
specific with MySQL - Rails isn’t trying to create any table - it’s on  
the MySQL side. Google “errno: 150” and have a look at the possible  
problems. There seems to be a pile of things that can pop this error.  
The second hit on google appears to use show innodb status to get more  
information out of the system (you would need to attempt the ALTER TABLE  
manually and you would obviously need to be using innodb).

Sorry can’t be more specific but this appears to be a catch all error  
that don’t reveal much about why it occurs…other then Rails isn’t the  
culprit 🙂

John W Higgins  
removed\_email\_address@domain.invalid

On 9/13/06, Will G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: I’ve added the  
following line to a migration file after all the create\_table  
statements. For the life of me, I can’t figure out what is wrong with  
it.

execute “ALTER TABLE users\_roles ADD CONSTRAINT FK\_UserRoles\_Users  
FOREIGN KEY(user\_id) REFERENCES users(user\_id)”

Here’s the error I’m getting.

execute(“ALTER TABLE users\_roles ADD CONSTRAINT FK\_UserRoles\_Users  
FOREIGN KEY(user\_id) REFERENCES users(user\_id)”)  
rake aborted!  
Mysql::Error: Can’t create table ‘./HoshinWeb\_dev/#sql-118c\_c.frm’  
(errno: 150): ALTER TABLE users\_roles ADD CONSTRAINT FK\_UserRoles\_Users  
FOREIGN KEY(user\_id) REFERENCES users(user\_id)

The code looks right, but it’s late and I’m sleep-deprived. What am I  
doing wrong? Why in the world is it trying to create a table here?

* * *

How low will we go? Check out Yahoo! Messenger’s low PC-to-Phone call  
rates.

* * *

Get your email and more, right on the new [Yahoo.com](http://Yahoo.com)

(What is best in life?) To crush your enemies, see them driven before  
you, and to hear the lamentations of the women. - Conan the Barbarian

* * *

Do you Yahoo!?  
Everyone is raving about the all-new Yahoo! Mail.
