Rails 2.1 Killed My MySql Foreign Keys

Posted on 02 July 2008 by Johannes Fahrenkrug. Tags: Programming Ruby rails
I'm porting a big Rails 1.2.x application to Rails 2.1 at the moment. Everything went smoothly so far, I used the rake deprecated task and watched out for gotchas, and replaced Rails 1.2.x pagination with will_paginate. When I tried to run my test suite, though, it threw up all over my terminal:
Mysql::Error: #HY000Can't create table './coresales_test/#sql-1702c_b.frm' (errno: 150): ALTER TABLE agents ADD CONSTRAINT agents_ibfk_1 FOREIGN KEY (location_id) REFERENCES locations (id)
It took me a bit to find the solution for this, but alas! I eventually did. To make a long story short: Rails 2.1 doesn't handle integer columns without a :limit attribute correctly by assuming a default limit of 11 and then turning that - not into int(11) - but into bigint(11). Foreign keys have to be of the same data type, though. So MySql rightly complains about it. The solution? Either use edge rails or use the monkey patch suggested in this extensive blog post about the problem. This was really quite annoying, but I'm glad that there are nice people out there providing patches and nice write-ups about such problems.
If this was useful for you, please take a minute and recommend me:
Recommend Me
Thank you!


John Trupiano said...

Hey, thanks for the link back to my blog post! I'm always ecstatic when I'm able to help someone else out.

Happy coding.

July 07, 2008 05:24 PM


Please keep it clean, everybody. Comments with profanity will be deleted.

blog comments powered by Disqus