Giter VIP home page Giter VIP logo

teitei-tk / modify-rails-activerecord-primary-key Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 28 KB

Modify Ruby on Rails ActiveRecord Default Primary Keys from BIGINT to INTEGER

Home Page: https://medium.com/@teitei_tk/modify-ruby-on-rails-activerecord-default-primary-keys-from-bigint-to-integer-d4c5c8eb4726

License: MIT License

Ruby 78.86% JavaScript 3.30% CSS 1.98% HTML 15.86%
rails rails5 mysql postgresql primary-key

modify-rails-activerecord-primary-key's Introduction

modify-rails-activerecord-primary-key

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.

How to

modify application model generator config

module ModifyRailsActiverecordPrimaryKey
  class Application < Rails::Application
    ......
    ......
    ......

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

about generated models

  • Hoge Model
    • primary key is bigint
  • Foo Model
    • primary key is integer

Database Schema

$ 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>

verbose logging

$ git log -p config/application.rb
commit f7d30da45f2fab369d4833038b2b8f9ed47e8aa1
Author: teitei-tk <[email protected]>
Date:   Mon Jun 11 23:02:32 2018 +0900

    modify default primary Keys from BIGINT to INTEGER

diff --git a/config/application.rb b/config/application.rb
index e402c24..7d55d27 100644
--- a/config/application.rb
+++ b/config/application.rb
commit f7d30da45f2fab369d4833038b2b8f9ed47e8aa1
Author: teitei-tk <[email protected]>
Date:   Mon Jun 11 23:02:32 2018 +0900

    modify default primary Keys from BIGINT to INTEGER

diff --git a/config/application.rb b/config/application.rb
index e402c24..7d55d27 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -15,5 +15,9 @@ module ModifyRailsActiverecordPrimaryKey
     # Application configuration can go into files in config/initializers
     # -- all .rb files in that directory are automatically loaded after loading
     # the framework and any gems in your application.
+
+    config.generators do |generator|
+      generator.orm :active_record, primary_key_type: :integer
+    end
   end
 end

commit 1a4d8ae7db3f832e4fb9c300f3d12be348eb2c97
Author: teitei-tk <[email protected]>
Date:   Mon Jun 11 22:57:00 2018 +0900

    install Rails 5.2 via Ruby 2.5.0

diff --git a/config/application.rb b/config/application.rb
new file mode 100644
index 0000000..e402c24
--- /dev/null
+++ b/config/application.rb
@@ -0,0 +1,19 @@
+require_relative 'boot'
+
+require 'rails/all'
+
+# Require the gems listed in Gemfile, including any gems
+# you've limited to :test, :development, or :production.
+Bundler.require(*Rails.groups)
+
+module ModifyRailsActiverecordPrimaryKey
+  class Application < Rails::Application
+    # Initialize configuration defaults for originally generated Rails version.
+    config.load_defaults 5.2
+
+    # Settings in config/environments/* take precedence over those specified here.
+    # Application configuration can go into files in config/initializers
+    # -- all .rb files in that directory are automatically loaded after loading
+    # the framework and any gems in your application.
+  end
+end

modify-rails-activerecord-primary-key's People

Contributors

teitei-tk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.