Help us understand the problem. What is going on with this article?

Rails5.1で変更された主キーのデフォルト型をBIGINTからINTEGERに戻す。

More than 1 year has passed since last update.

Rails5.1にてPostgreSQLとMySQLのActiveRecordのPrimaryKeyの型がIntegerからBigintに変更されました。

PullRequest

https://github.com/rails/rails/pull/26266

Rails 5.1 Release Note

9.3 主な変更点
主キーのデフォルト型をBIGINTに変更 (Pull Request)

(英語のリリースノート)

なぜBigintからintegerに戻すのか

新規のRails ProjectではBigintでも構わないと思うのですが、
Rails5.1以前に作られた既存のプロジェクトで新たにActiveRecord modelを作る際にBigintになると不都合が多いと思います。
※ 既存のアーキテクチャとは違っていて一貫性が取れないなど。

どうやって変えるか

config/application.rb にてgeneratorの設定を変更します。

module ExampleApplication
  class Application < Rails::Application
    ......
    ......
    ......

    config.generators do |generator|
      generator.orm :active_record, primary_key_type: :integer
    end
  end
end

ActiveRecordのソースコードを眺めると、primary_key_type methodにて型を確認していることが見えます。

def primary_key_type
  key_type = options[:primary_key_type]
  ", id: :#{key_type}" if key_type
end

対応した結果

ormのprimary_key_typeがdefaultの場合(bigintのまま

class CreateHoges < ActiveRecord::Migration[5.2]
  def change
    create_table :hoges do |t|

      t.timestamps
    end
  end
end

ormのprimary_key_typeのdefaultの型をintegerに変更

class CreateFoos < ActiveRecord::Migration[5.2]
  def change
    create_table :foos, id: :integer do |t|

      t.timestamps
    end
  end
end

create_tableにてidがintegerになっているのが確認できると思います。

db:migrateをして確認

$ bin/rails db:migrate
$ 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>

bigintとintegerになっていることが確認できます。

内容をGithubに公開していますので、不明点があれば確認をお願いします。

teitei-tk/modify-rails-activerecord-primary-key

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした