Initial problem
In my Rails application I need to work with data from a legacy database, with large numbers as primary keys. Numbers that exceed the limits of both MySQL's and PostgreSQL's 4-byte INT or INTEGER data type. But that's exactly the data type that Rails' adapters translate "integer
" into.
My workaround
That was not an issue in development, since SQLite has only one integer type accepting large numbers. But for staging/production (i.e. for use with anything but SQLite), I had to manually edit the schema.rb
file to replace t.integer "id"
with t.column "id", 'BIGINT'
. Then load the schema, import the data, and it works.
The problem with the workaround
Now, everytime I run a Rails migration to make a minor change to the database schema, the entire schema.rb
file is automatically re-generated, and all my id
columns are just t.integer
again. As before, this isn't a problem in development (SQLite), but it probably will be in production, won't it? I'd hate to have to drop the database, load the schema and reimport all data every time I need to add or rename a column – I'd much rather use migrations for this.
The workaround for me thus far has been to make a backup copy of schema.rb
before migrating, and then to merge the additions made by the migration with my own ones. Does anyone have a better idea?