{"id":5652,"date":"2015-02-25T09:00:40","date_gmt":"2015-02-25T00:00:40","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=5652"},"modified":"2018-11-14T16:33:49","modified_gmt":"2018-11-14T07:33:49","slug":"postgresql-and-rails-use-bigserial-id","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2015\/02\/25\/postgresql-and-rails-use-bigserial-id\/","title":{"rendered":"PostgreSQL+Rails\u3067\u30c6\u30fc\u30d6\u30eb\u306eID\u306bBIGSERIAL\u3092\u4f7f\u3046"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002<\/p>\n<p>Rails\u3068Postgresql\u306e\u7d44\u307f\u5408\u308f\u305b\u3067migration\u3092\u901a\u3057\u3066\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3068\u304d\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u3001ID\u306e\u30c7\u30fc\u30bf\u578b\u306fSERIAL(INT)\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u7279\u306b\u30c7\u30fc\u30bf\u91cf\u304c\u591a\u304f\u306a\u3044\u5834\u5408\u306b\u306f\u554f\u984c\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u30c7\u30fc\u30bf\u91cf\u591a\u304f\u306a\u3063\u305f\u3068\u304d\u306bINT\u306e\u4e0a\u9650\u5024(2147483647)\u3092\u8d85\u3048\u3066\u3057\u307e\u3046\u3068\u30c7\u30fc\u30bf\u304c\u30a4\u30f3\u30b5\u30fc\u30c8\u3067\u304d\u306a\u304f\u306a\u308a\u3001\u5927\u5909\u306a\u4e8b\u614b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u306f\u3001\u5148\u65e5\u3042\u308b\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u3001\u30c6\u30fc\u30d6\u30eb\u306eID\u304cINT\u306e\u4e0a\u9650\u3092\u8d85\u3048\u3066\u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3057\u307e\u3057\u305f\u3002ID\u3092BIGINT\u306b\u5909\u66f4\u3059\u308b\u3053\u3068\u3067\u5bfe\u5fdc\u3067\u304d\u305f\u306e\u3067\u826f\u304b\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u305d\u3082\u305d\u3082\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\u6642\u306bBIGINT\u306b\u3057\u3066\u304a\u3051\u3070\u554f\u984c\u306f\u767a\u751f\u3057\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n<p>\u3067\u306f\u3001\u3069\u3046\u3084\u3063\u305f\u3089\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\u6642\u306bBIGINT(PostgreSQL\u306a\u306e\u3067BIGSERIAL)\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<h2>create_table\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3067ID\u306e\u578b\u3092\u6307\u5b9a\u3059\u308b<\/h2>\n<p>create_table\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u306b:id\u3068\u3044\u3046\u306e\u304c\u3042\u308b\u306e\u3067\u3001\u305d\u308c\u3092:bigserial\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true \" ># db\/migrate\/20150220014057_create_histories.rb\r\nclass CreateHistories < ActiveRecord::Migration\r\n  def change\r\n    create_table :histories, {id: :bigserial} do |t|\r\n      t.integer :user_id\r\n    end\r\n  end\r\nend\r\n<\/pre>\n<p>\u3059\u3054\u3044\u30b7\u30f3\u30d7\u30eb\uff01<\/p>\n<h2>\u78ba\u8a8d<\/h2>\n<p>rake\u3092\u5b9f\u884c\u3057\u3066\u3001<\/p>\n<pre class=\"lang:sh decode:true \">% bundle exec rake db:migrate\r\n== 20150220014057 CreateHistories: migrating ==================================\r\n-- create_table(:histories, {:id=>:bigserial})\r\n   -> 0.0148s\r\n== 20150220014057 CreateHistories: migrated (0.0148s) =========================\r\n<\/pre>\n<p>psql\u3067\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \">% psql -c '\\d histories*'\r\n                          \u30c6\u30fc\u30d6\u30eb \"public.histories\"\r\n \u30ab\u30e9\u30e0  |   \u578b    |                          \u4fee\u98fe\u8a9e\r\n---------+---------+-----------------------------------------------------------\r\n id      | bigint  | not null \u30c7\u30d5\u30a9\u30eb\u30c8 nextval('histories_id_seq'::regclass)\r\n user_id | integer |\r\n\u30a4\u30f3\u30c7\u30c3\u30af\u30b9:\r\n    \"histories_pkey\" PRIMARY KEY, btree (id)\r\n\r\n     \u30b7\u30fc\u30b1\u30f3\u30b9 \"public.histories_id_seq\"\r\n    \u30ab\u30e9\u30e0     |   \u578b    |         \u5024\r\n---------------+---------+---------------------\r\n sequence_name | name    | histories_id_seq\r\n last_value    | bigint  | 1\r\n start_value   | bigint  | 1\r\n increment_by  | bigint  | 1\r\n max_value     | bigint  | 9223372036854775807\r\n min_value     | bigint  | 1\r\n cache_value   | bigint  | 1\r\n log_cnt       | bigint  | 0\r\n is_cycled     | boolean | f\r\n is_called     | boolean | f\r\n\r\n\u30a4\u30f3\u30c7\u30c3\u30af\u30b9 \"public.histories_pkey\"\r\n \u30ab\u30e9\u30e0 |   \u578b\r\n--------+--------\r\n id     | bigint\r\n\u30d7\u30e9\u30a4\u30de\u30ea\u30ad\u30fc, btree, \u30c6\u30fc\u30d6\u30eb \"public.histories\" \u7528\r\n<\/pre>\n<p>\u3061\u3083\u3093\u3068\u3001histories\u306eid\u306e\u578b\u304cbigint\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u306d\uff01<\/p>\n<h2>\u30b3\u30fc\u30c9\u3092\u898b\u3066\u3044\u3066\u6c17\u3065\u3044\u305f<\/h2>\n<p>create_table\u306e\u6319\u52d5\u306f\u3069\u3046\u306a\u3063\u3066\u3044\u308b\u306e\u304b\u3001\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n<a href=\"https:\/\/github.com\/rails\/rails\/blob\/master\/activerecord\/lib\/active_record\/connection_adapters\/abstract\/schema_statements.rb#L191\">ActiveRecord::ConnectionAdapters::SchemaStatements<\/a><\/p>\n<pre class=\"lang:ruby decode:true \" >\r\n      def create_table(table_name, options = {})\r\n        td = create_table_definition table_name, options[:temporary], options[:options], options[:as]\r\n\r\n        if options[:id] != false && !options[:as]\r\n          pk = options.fetch(:primary_key) do\r\n            Base.get_primary_key table_name.to_s.singularize\r\n          end\r\n\r\n          td.primary_key pk, options.fetch(:id, :primary_key), options\r\n        end\r\n\r\n        yield td if block_given?\r\n\r\n        if options[:force] && table_exists?(table_name)\r\n          drop_table(table_name, options)\r\n        end\r\n\r\n        result = execute schema_creation.accept td\r\n\r\n        unless supports_indexes_in_create?\r\n          td.indexes.each_pair do |column_name, index_options|\r\n            add_index(table_name, column_name, index_options)\r\n          end\r\n        end\r\n\r\n        td.foreign_keys.each_pair do |other_table_name, foreign_key_options|\r\n          add_foreign_key(table_name, other_table_name, foreign_key_options)\r\n        end\r\n\r\n        result\r\n      end\r\n<\/pre>\n<p>options[:id]\u304cfalse\u3067\u306f\u306a\u3044\u3001\u304b\u3064\u3001options[:as]\u304cfalse(nil)\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u304d\u306b\u3001create_table_definition\u3067\u53d6\u5f97\u3057\u305f\u3001TableDefinition\u306eprimary_key\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002<br \/>\n\u3067\u306f\u3001primary_key\u306f\u3069\u3046\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u306a\u306e\u3067\u3057\u3087\u3046\u304b\u3002\uff08\u3068\u3044\u3046\u304b\u3001primary_key\u3063\u3066\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3042\u308b\u3093\u3067\u3059\u306d\u3002\uff09<\/p>\n<p><a href=\"https:\/\/github.com\/rails\/rails\/blob\/master\/activerecord\/lib\/active_record\/connection_adapters\/postgresql\/schema_definitions.rb#L127\">ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnMethods::TableDefinition<\/a><\/p>\n<pre class=\"lang:ruby decode:true \" >\r\n        def primary_key(name, type = :primary_key, options = {})\r\n          options[:default] = options.fetch(:default, 'uuid_generate_v4()') if type == :uuid\r\n          super\r\n        end\r\n<\/pre>\n<p>uuid\u306b\u95a2\u3057\u3066\u306e\u8a18\u8ff0\u304c\u3042\u3063\u3066\u3001super\u3092\u547c\u3073\u51fa\u3057\u3066\u3044\u307e\u3059\u306d\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/rails\/rails\/blob\/master\/activerecord\/lib\/active_record\/connection_adapters\/abstract\/schema_definitions.rb#L186\">ActiveRecord::ConnectionAdapters::TableDefinition<\/a><\/p>\n<pre class=\"lang:ruby decode:true \" >\r\n      def primary_key(name, type = :primary_key, options = {})\r\n        column(name, type, options.merge(:primary_key => true))\r\n      end\r\n<\/pre>\n<p>column\u30e1\u30bd\u30c3\u30c9\u306e\u30e9\u30c3\u30d1\u30fc\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\ntype\u304c :primary_key\u306b\u306a\u3063\u3066\u3044\u3066\u3001option\u306b :primary_key => true \u3092\u8ffd\u52a0\u3057\u3066\u3044\u308b\u3060\u3051\u3067\u3059\u306d\u3002<br \/>\n\u3064\u307e\u308atype\u306e\u3068\u3053\u308d\u306b\u3001:bigserial\u304c\u6e21\u305b\u305f\u3089\u306a\u3093\u3068\u304b\u306a\u308a\u305d\u3046\u3067\u3059\u3002<\/p>\n<p>\u6539\u3081\u3066create_table\u306e\u6319\u52d5\u3092\u78ba\u8a8d\u3059\u308b\u3068\u3001<\/p>\n<pre class=\"lang:ruby decode:true \" >        if options[:id] != false && !options[:as]\r\n          pk = options.fetch(:primary_key) do\r\n            Base.get_primary_key table_name.to_s.singularize\r\n          end\r\n\r\n          td.primary_key pk, options.fetch(:id, :primary_key), options\r\n        end<\/pre>\n<p>options.fetch(:id, :primary_key) \u306e\u623b\u308a\u5024\u304c\u3001column\u30e1\u30bd\u30c3\u30c9\u306etype\u306b\u6e21\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001:bigserial\u304c\u30db\u30f3\u30c8\u306b\u4f7f\u3048\u308b\u306e\u304b\u3002PostgreSQLAdapter \u306e NATIVE_DATABASE_TYPES \u3092\u898b\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/rails\/rails\/blob\/master\/activerecord\/lib\/active_record\/connection_adapters\/postgresql_adapter.rb#L79\">ActiveRecord::ConnectionAdapters::PostgreSQLAdapter <\/a><\/p>\n<pre class=\"lang:ruby decode:true \" >\r\n      NATIVE_DATABASE_TYPES = {\r\n        primary_key: \"serial primary key\",\r\n        bigserial: \"bigserial\",\r\n        string:      { name: \"character varying\" },\r\n...snip\r\n<\/pre>\n<p>\u554f\u984c\u306a\u3044\u3088\u3046\u3067\u3059\u306d\u3002<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u6700\u521d\u306b\u8a18\u8f09\u3057\u305f\u901a\u308a\u3067\u3059\u304c\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308b\u3068BIGSERIAL\u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true \" ># db\/migrate\/20150220014057_create_histories.rb\r\nclass CreateHistories < ActiveRecord::Migration\r\n  def change\r\n    create_table :histories, {id: :bigserial} do |t|\r\n      t.integer :user_id\r\n    end\r\n  end\r\nend\r\n<\/pre>\n<p>\u307e\u305f\u306f\u3001\u3053\u3046\u3044\u3046\u66f8\u304d\u65b9\u3082\u5927\u4e08\u592b\u306a\u3088\u3046\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"lang:ruby decode:true \" >\r\nclass CreateHistories < ActiveRecord::Migration\r\n  def change\r\n    create_table :histories, {id: false} do |t|\r\n      t.primary_key :id, :bigserial\r\n      t.integer :user_id\r\n    end\r\n  end\r\nend\r\n<\/pre>\n<p>\u3061\u306a\u307f\u306b\u3002<\/p>\n<p><a href=\"http:\/\/apidock.com\/rails\/ActiveRecord\/ConnectionAdapters\/SchemaStatements\/create_table\">create_table (ActiveRecord::ConnectionAdapters::SchemaStatements) - APIdock<\/a><br \/>\n\u2191\u3053\u3061\u3089\u306ecreate_table\u306e\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u3092\u898b\u308b\u3068\u3001<\/p>\n<blockquote><p>Whether to automatically add a primary key column. Defaults to true. Join tables for has_and_belongs_to_many should set it to false.<\/p><\/blockquote>\n<p>\u3068\u66f8\u3044\u3066\u3042\u3063\u3066\u3001\u3044\u304b\u306b\u3082true\u304bfalse\u3057\u304b\u8a2d\u5b9a\u3067\u304d\u306a\u3044\u3088\u3046\u306a\u611f\u3058\u304c\u3057\u307e\u3059\u3002<\/p>\n<p>\u3067\u3082\u3001\u5b9f\u969b\u306b\u306f\u578b\u306e\u6307\u5b9a\u304c\u3067\u304d\u308b\u3001\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u306d\u3002\u306a\u3093\u3068\u304b\u51fa\u6765\u306a\u3044\u304b\u3068\u601d\u3063\u3066\u30b3\u30fc\u30c9\u3092\u898b\u305f\u306e\u3067\u3059\u304c\u3001\u5927\u5909\u52c9\u5f37\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u30b3\u30fc\u30c9\u898b\u308b\u306e\u5927\u4e8b\u3067\u3059\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002<\/p>\n<p>Rails\u3068Postgresql\u306e\u7d44\u307f\u5408\u308f\u305b\u3067migration\u3092\u901a\u3057\u3066\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3068\u304d\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u3001ID\u306e\u30c7\u30fc\u30bf\u578b\u306fSERIAL(INT)\u306b\u306a\u308a\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2015\/02\/25\/postgresql-and-rails-use-bigserial-id\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[206,22],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/5652"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=5652"}],"version-history":[{"count":30,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/5652\/revisions"}],"predecessor-version":[{"id":5687,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/5652\/revisions\/5687"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=5652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=5652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=5652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}