How to Use Different Primary Key in Rails with MySQL?
I had a situation where I had to use different primary key column name over the default one (id) Rails provide.
As per the official guide:
create_tablewill create a primary key called
id. You can change the name of the primary key with the
:primary_keyoption (don’t forget to update the corresponding model) or, if you don’t want a primary key at all, you can pass the option id: false.
So I set
:primary_key option in
create_table and it worked.
Here is the Generated DDL in MySQL
Now I need the primary key column to be type of bigint(20) instead of default int(11).
I created another migration to modify column type and width of primary key.
This migration correctly changes the type but drops
AUTO_INCREMENT from the table definition.
I looked around again and landed to an issue which helped me to fix the issue.
One more change would require to make it work is to set
primary_key into your model.
Consulting is free – let us help you grow!
Choose Your Language
- Digital Marketing
- IT Consulting
- Project Management