Why did Rails 5 changed “index” to “foreign key”?












6















If you had this in Rails 4:



t.references :event, index: true


Now you could use foreign_key instead of index in Rails 5. I don't quite understand WHY they decided to do this, since the functionality remains the same, what you're adding is an INDEX, not a FOREIGN KEY to that column.










share|improve this question




















  • 3





    I honestly don't understand your question, the index option adds a database index, the foreign key one adds a database foreign key.

    – j-dexx
    Sep 29 '16 at 12:13
















6















If you had this in Rails 4:



t.references :event, index: true


Now you could use foreign_key instead of index in Rails 5. I don't quite understand WHY they decided to do this, since the functionality remains the same, what you're adding is an INDEX, not a FOREIGN KEY to that column.










share|improve this question




















  • 3





    I honestly don't understand your question, the index option adds a database index, the foreign key one adds a database foreign key.

    – j-dexx
    Sep 29 '16 at 12:13














6












6








6


1






If you had this in Rails 4:



t.references :event, index: true


Now you could use foreign_key instead of index in Rails 5. I don't quite understand WHY they decided to do this, since the functionality remains the same, what you're adding is an INDEX, not a FOREIGN KEY to that column.










share|improve this question
















If you had this in Rails 4:



t.references :event, index: true


Now you could use foreign_key instead of index in Rails 5. I don't quite understand WHY they decided to do this, since the functionality remains the same, what you're adding is an INDEX, not a FOREIGN KEY to that column.







ruby-on-rails foreign-keys primary-key ruby-on-rails-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 29 '16 at 13:31









Ajay Barot

1,43111833




1,43111833










asked Sep 29 '16 at 12:11









anemaria20anemaria20

537722




537722








  • 3





    I honestly don't understand your question, the index option adds a database index, the foreign key one adds a database foreign key.

    – j-dexx
    Sep 29 '16 at 12:13














  • 3





    I honestly don't understand your question, the index option adds a database index, the foreign key one adds a database foreign key.

    – j-dexx
    Sep 29 '16 at 12:13








3




3





I honestly don't understand your question, the index option adds a database index, the foreign key one adds a database foreign key.

– j-dexx
Sep 29 '16 at 12:13





I honestly don't understand your question, the index option adds a database index, the foreign key one adds a database foreign key.

– j-dexx
Sep 29 '16 at 12:13












3 Answers
3






active

oldest

votes


















26














In Rails 5 - when we reference a model, index on the foreign_key is automatically created.



Migration API has changed in Rails 5 -



Rails 5 has changed migration API because of which even though null: false options is not passed to timestamps when migrations are run then not null is automatically added for timestamps.



Similarly, we want indexes for referenced columns in almost all cases. So Rails 5 does not need references to have index: true. When migrations are run then index is automatically created.



As an example - (Copying from http://blog.bigbinary.com/2016/03/01/migrations-are-versioned-in-rails-5.html)



When you run rails g model Task user:references



Rails 4 would generate



class CreateTasks < ActiveRecord::Migration
def change
create_table :tasks do |t|
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end


And rails 5 would generate



class CreateTasks < ActiveRecord::Migration[5.0]
def change
create_table :tasks do |t|
t.references :user, foreign_key: true
t.timestamps
end
end
end





share|improve this answer
























  • Very well detailled answer. BUT, what if for whatever reason, I don't want the reference to be indexed? Or was it still indexed implicitly in Rails 4 even if I only have t.references :user, foreign_key: true ? --- And now, I wonder why they didn't even omit foreign_key: true too.

    – Asme Just
    Jul 29 '17 at 6:24



















6














The index and foreign_key are different concepts, even if in Rails 5. So it's wrong to say the rails 5 changed “index” to “foreign key”.



The change from Rails 4 to Rails 5 is that the index option defaults to true, so you don't need to set it explicitly.



Method add_reference in rails 4.2.5




:index



Add an appropriate index. Defaults to false.






Method add_reference in rails 5.2




:index



Add an appropriate index. Defaults to true. See add_index for usage of this option.




That's why when you generate the references in rails 5 migration, you didn't see the index: true, because it's default.






share|improve this answer

































    5














    foreign_key and index are completely different things (as you may judge from their names).



    So nothing is being changed, you still can use two.



    You can check out these docs for some more info on establishing associations in migrations.






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39769981%2fwhy-did-rails-5-changed-index-to-foreign-key%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      26














      In Rails 5 - when we reference a model, index on the foreign_key is automatically created.



      Migration API has changed in Rails 5 -



      Rails 5 has changed migration API because of which even though null: false options is not passed to timestamps when migrations are run then not null is automatically added for timestamps.



      Similarly, we want indexes for referenced columns in almost all cases. So Rails 5 does not need references to have index: true. When migrations are run then index is automatically created.



      As an example - (Copying from http://blog.bigbinary.com/2016/03/01/migrations-are-versioned-in-rails-5.html)



      When you run rails g model Task user:references



      Rails 4 would generate



      class CreateTasks < ActiveRecord::Migration
      def change
      create_table :tasks do |t|
      t.references :user, index: true, foreign_key: true
      t.timestamps null: false
      end
      end
      end


      And rails 5 would generate



      class CreateTasks < ActiveRecord::Migration[5.0]
      def change
      create_table :tasks do |t|
      t.references :user, foreign_key: true
      t.timestamps
      end
      end
      end





      share|improve this answer
























      • Very well detailled answer. BUT, what if for whatever reason, I don't want the reference to be indexed? Or was it still indexed implicitly in Rails 4 even if I only have t.references :user, foreign_key: true ? --- And now, I wonder why they didn't even omit foreign_key: true too.

        – Asme Just
        Jul 29 '17 at 6:24
















      26














      In Rails 5 - when we reference a model, index on the foreign_key is automatically created.



      Migration API has changed in Rails 5 -



      Rails 5 has changed migration API because of which even though null: false options is not passed to timestamps when migrations are run then not null is automatically added for timestamps.



      Similarly, we want indexes for referenced columns in almost all cases. So Rails 5 does not need references to have index: true. When migrations are run then index is automatically created.



      As an example - (Copying from http://blog.bigbinary.com/2016/03/01/migrations-are-versioned-in-rails-5.html)



      When you run rails g model Task user:references



      Rails 4 would generate



      class CreateTasks < ActiveRecord::Migration
      def change
      create_table :tasks do |t|
      t.references :user, index: true, foreign_key: true
      t.timestamps null: false
      end
      end
      end


      And rails 5 would generate



      class CreateTasks < ActiveRecord::Migration[5.0]
      def change
      create_table :tasks do |t|
      t.references :user, foreign_key: true
      t.timestamps
      end
      end
      end





      share|improve this answer
























      • Very well detailled answer. BUT, what if for whatever reason, I don't want the reference to be indexed? Or was it still indexed implicitly in Rails 4 even if I only have t.references :user, foreign_key: true ? --- And now, I wonder why they didn't even omit foreign_key: true too.

        – Asme Just
        Jul 29 '17 at 6:24














      26












      26








      26







      In Rails 5 - when we reference a model, index on the foreign_key is automatically created.



      Migration API has changed in Rails 5 -



      Rails 5 has changed migration API because of which even though null: false options is not passed to timestamps when migrations are run then not null is automatically added for timestamps.



      Similarly, we want indexes for referenced columns in almost all cases. So Rails 5 does not need references to have index: true. When migrations are run then index is automatically created.



      As an example - (Copying from http://blog.bigbinary.com/2016/03/01/migrations-are-versioned-in-rails-5.html)



      When you run rails g model Task user:references



      Rails 4 would generate



      class CreateTasks < ActiveRecord::Migration
      def change
      create_table :tasks do |t|
      t.references :user, index: true, foreign_key: true
      t.timestamps null: false
      end
      end
      end


      And rails 5 would generate



      class CreateTasks < ActiveRecord::Migration[5.0]
      def change
      create_table :tasks do |t|
      t.references :user, foreign_key: true
      t.timestamps
      end
      end
      end





      share|improve this answer













      In Rails 5 - when we reference a model, index on the foreign_key is automatically created.



      Migration API has changed in Rails 5 -



      Rails 5 has changed migration API because of which even though null: false options is not passed to timestamps when migrations are run then not null is automatically added for timestamps.



      Similarly, we want indexes for referenced columns in almost all cases. So Rails 5 does not need references to have index: true. When migrations are run then index is automatically created.



      As an example - (Copying from http://blog.bigbinary.com/2016/03/01/migrations-are-versioned-in-rails-5.html)



      When you run rails g model Task user:references



      Rails 4 would generate



      class CreateTasks < ActiveRecord::Migration
      def change
      create_table :tasks do |t|
      t.references :user, index: true, foreign_key: true
      t.timestamps null: false
      end
      end
      end


      And rails 5 would generate



      class CreateTasks < ActiveRecord::Migration[5.0]
      def change
      create_table :tasks do |t|
      t.references :user, foreign_key: true
      t.timestamps
      end
      end
      end






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Sep 29 '16 at 12:23









      Pramod SolankyPramod Solanky

      1,3581116




      1,3581116













      • Very well detailled answer. BUT, what if for whatever reason, I don't want the reference to be indexed? Or was it still indexed implicitly in Rails 4 even if I only have t.references :user, foreign_key: true ? --- And now, I wonder why they didn't even omit foreign_key: true too.

        – Asme Just
        Jul 29 '17 at 6:24



















      • Very well detailled answer. BUT, what if for whatever reason, I don't want the reference to be indexed? Or was it still indexed implicitly in Rails 4 even if I only have t.references :user, foreign_key: true ? --- And now, I wonder why they didn't even omit foreign_key: true too.

        – Asme Just
        Jul 29 '17 at 6:24

















      Very well detailled answer. BUT, what if for whatever reason, I don't want the reference to be indexed? Or was it still indexed implicitly in Rails 4 even if I only have t.references :user, foreign_key: true ? --- And now, I wonder why they didn't even omit foreign_key: true too.

      – Asme Just
      Jul 29 '17 at 6:24





      Very well detailled answer. BUT, what if for whatever reason, I don't want the reference to be indexed? Or was it still indexed implicitly in Rails 4 even if I only have t.references :user, foreign_key: true ? --- And now, I wonder why they didn't even omit foreign_key: true too.

      – Asme Just
      Jul 29 '17 at 6:24













      6














      The index and foreign_key are different concepts, even if in Rails 5. So it's wrong to say the rails 5 changed “index” to “foreign key”.



      The change from Rails 4 to Rails 5 is that the index option defaults to true, so you don't need to set it explicitly.



      Method add_reference in rails 4.2.5




      :index



      Add an appropriate index. Defaults to false.






      Method add_reference in rails 5.2




      :index



      Add an appropriate index. Defaults to true. See add_index for usage of this option.




      That's why when you generate the references in rails 5 migration, you didn't see the index: true, because it's default.






      share|improve this answer






























        6














        The index and foreign_key are different concepts, even if in Rails 5. So it's wrong to say the rails 5 changed “index” to “foreign key”.



        The change from Rails 4 to Rails 5 is that the index option defaults to true, so you don't need to set it explicitly.



        Method add_reference in rails 4.2.5




        :index



        Add an appropriate index. Defaults to false.






        Method add_reference in rails 5.2




        :index



        Add an appropriate index. Defaults to true. See add_index for usage of this option.




        That's why when you generate the references in rails 5 migration, you didn't see the index: true, because it's default.






        share|improve this answer




























          6












          6








          6







          The index and foreign_key are different concepts, even if in Rails 5. So it's wrong to say the rails 5 changed “index” to “foreign key”.



          The change from Rails 4 to Rails 5 is that the index option defaults to true, so you don't need to set it explicitly.



          Method add_reference in rails 4.2.5




          :index



          Add an appropriate index. Defaults to false.






          Method add_reference in rails 5.2




          :index



          Add an appropriate index. Defaults to true. See add_index for usage of this option.




          That's why when you generate the references in rails 5 migration, you didn't see the index: true, because it's default.






          share|improve this answer















          The index and foreign_key are different concepts, even if in Rails 5. So it's wrong to say the rails 5 changed “index” to “foreign key”.



          The change from Rails 4 to Rails 5 is that the index option defaults to true, so you don't need to set it explicitly.



          Method add_reference in rails 4.2.5




          :index



          Add an appropriate index. Defaults to false.






          Method add_reference in rails 5.2




          :index



          Add an appropriate index. Defaults to true. See add_index for usage of this option.




          That's why when you generate the references in rails 5 migration, you didn't see the index: true, because it's default.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 21:30









          Meekohi

          6,68643948




          6,68643948










          answered Jun 6 '18 at 23:28









          StephenStephen

          1,499918




          1,499918























              5














              foreign_key and index are completely different things (as you may judge from their names).



              So nothing is being changed, you still can use two.



              You can check out these docs for some more info on establishing associations in migrations.






              share|improve this answer




























                5














                foreign_key and index are completely different things (as you may judge from their names).



                So nothing is being changed, you still can use two.



                You can check out these docs for some more info on establishing associations in migrations.






                share|improve this answer


























                  5












                  5








                  5







                  foreign_key and index are completely different things (as you may judge from their names).



                  So nothing is being changed, you still can use two.



                  You can check out these docs for some more info on establishing associations in migrations.






                  share|improve this answer













                  foreign_key and index are completely different things (as you may judge from their names).



                  So nothing is being changed, you still can use two.



                  You can check out these docs for some more info on establishing associations in migrations.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 29 '16 at 12:14









                  Andrey DeinekoAndrey Deineko

                  40.5k85695




                  40.5k85695






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39769981%2fwhy-did-rails-5-changed-index-to-foreign-key%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      How to fix TextFormField cause rebuild widget in Flutter