A hint when working with Rails Migrations and legacy databases (ID columns)

Had an interesting problem recently when writing some Rail migrations scripts for an old non-Rails MySQL database. After writing the migration and running it via Rake we found that it was erroring with a complaint about the primary key.

We checked that the table had an id column, it did but instead of id it was ID. ActiveRecord is very specific in what it looks for and so was not viewing this ID column as the primary key that it required when adding new rows to the table.

A quick lookup in the ActiveRecord documentation pointed out the solution: use set_primary_key in your ActiveRecord class:

class SomeLegacyTable < ActiveRecord::Base
set_primary_key :ID
end
Technorati Tags: , , , ,

No comments: