7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-06-11

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

PullRequest

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

7
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?