Picture of admin

"superclass mismatch" and migrations in Rails

  • Posted By admin on January 24, 2006

Came across this little head-scratcher today: added a new migration to our application and, when trying to run it, Rake aborted the migration with an error of “superclass mismatch”. After poking around trying to find out if we accidentally introduced a second copy of ActiveRecord::Base somewhere, I discovered the actual answer.

Imagine if you will that our application has a model class called Foo. This means we have a class:

class Foo < ActiveRecord::Base
# etc.
end

Somewhere along the line, somebody wanted to update the schema for Foo and ran this command:

> script/generate migration Foo

Which gives us, buried in the db/migration folder:

class Foo < ActiveRecord::Migration
# etc.
end

Thus, the type mismatch problem as Ruby discovers two completely different classes called Foo with mismatching superclasses. This isn’t the odd part. The odd part, which I haven’t figured out yet, is why we only saw the problem now. You see, we just added migration #044. The offending migration was #007.

When I figure out why it worked until now, I’ll report back. Until then, if you ever get a superclass mismatch error in a rake run, look at your migrations.

Comments
  1. EconomysizegeekJanuary 24, 2006 @ 10:19 PM
    I've run into the same kind of problem - on the team I work with - you name the migration after the table - so the plural - which prevents some of the name conflicts. If it is going to be named after a given class it should have a modifier to explain what is happening. It's kind of annoying that the two namespaces collide...
  2. JulesApril 07, 2006 @ 10:49 AM
    I think you only saw the problem now because Rails loads the classes lazily. So the Foo model won't be loaded until you use it.
  3. Final BlockApril 29, 2006 @ 06:18 PM
    I was very dissapointed of this :)
  4. Strain Share Engineer Online TradingMay 07, 2006 @ 11:03 AM
    But I'm not sure why :)
  5. Oxygenise Mattresses For Sleeping BagsMay 07, 2006 @ 06:26 PM
    I'm working :)
  6. Free Downloadable Slot GamesMay 08, 2006 @ 02:57 PM
    I'm working!
  7. Prescription Favourite Medication AustraliaMay 09, 2006 @ 01:56 AM
    I think it would be usefull for other users also...
  8. Laser Tag SystemMay 09, 2006 @ 09:28 PM
    I was very dissapointed of this!
  9. Openmg Juke BoxfulMay 10, 2006 @ 04:38 PM
    Well done, nice instructions.
  10. Air Fluidized Imperativeness Ease MattressesMay 10, 2006 @ 09:35 PM
    I think it would be usefull for other users also!
  11. Undecided BraMay 11, 2006 @ 01:34 PM
    Just thought I'd make a note about a problem!
  12. Denison House TreeMay 17, 2006 @ 09:39 PM
    I use Firefox in Ubuntu!
  13. tebojoklApril 29, 2007 @ 04:38 PM