Change Primary Key Column Size(Type) Using Rails Migration with MySQL
By default rails set primary key column to int(11). But we had a situation where we need to increase size to
Now, Rails’ generic migration API doesn’t support that out of the box. So first let Rails generate PK column automatically. Next, we have to explicitly modify column using MySQL specific query using execute method.
Notice here, we have used
up/down method instead of
change. The reason being,
execute method executes raw SQLs which is database dependent. So Rails has no idea how to reverse that operation and so it will throw
Active Record::IrreversibleMigration when you rollback the migration.
At BoTree Technologies, we build enterprise applications with our RoR team of 25+ engineers.
Consulting is free – let us help you grow!
Choose Your Language
- Digital Marketing
- IT Consulting
- Project Management