Another migration problem

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.

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
:slight_smile:

John W Higgins
[email protected]

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. [email protected] 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 :slight_smile:

John W Higgins
[email protected]

On 9/13/06, Will G. [email protected] 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

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

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. [email protected] 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. [email protected] 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 :slight_smile:

John W Higgins
[email protected]

On 9/13/06, Will G. [email protected] 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

(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.