Modify Ruby on Rails ActiveRecord Default Primary Keys from BIGINT to INTEGER
why it’s?
Rails5.1 or later is Breaking Changed ActiveRecord Default Primary Keys to BIGINT on MySQL and PostgreSQL. here a reset the Default Primary Key to an INTEGER.
Rails5.1 ReleaseNote
Change Default Primary Keys to BIGINT. (Pull Request)
Why to switch back to integer from Bigint?
I think that Bigint is OK in the new Rails Project,
I think that there are many disadvantages when becoming Bigint when creating a new ActiveRecord model with an existing project created before Rails 5.1.
How to
modify application model generator config
module ExampleApp
class Application < Rails::Application
config.generators do |generator|
generator.orm :active_record, primary_key_type: :integer
end
end
end
Looking at the source code of Active Record, you can see that you are confirming the type with primary_key_type method.
def primary_key_type
key_type = options[:primary_key_type]
", id: :#{key_type}" if key_type
end
If orm’s primary_key_type is default (leave as bigint
class CreateHoges < ActiveRecord::Migration[5.2]
def change
create_table :hoges do |t|
t.timestamps
end
end
end
Changed default type of primary_key_type of orm to integer
class CreateFoos < ActiveRecord::Migration[5.2]
def change
create_table :foos, id: :integer do |t|
t.timestamps
end
end
end
I think that you can see that id is integer in create_table.
result.
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.15 Homebrew
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use modify-rails-activerecord-primary-key_development;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show create table hoges;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| hoges | CREATE TABLE `hoges` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table foos;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| foos | CREATE TABLE `foos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
It can be changed that it is bigint and integer.
Since the contents are released to Github, please confirm if there is an uncertain point.
thank’s :)