Why did Rails 5 change the "index" to the "foreign key"?

If you had this in Rails 4:

t.references :event, index: true

Now you can use Rails 5 foreign_keyinstead index. I don’t quite understand WHY they decided to do this, since the functionality remains the same, what you add is INDEX, not FOREIGN KEY to this column.

+4
source share
2 answers

In Rails 5 - when we refer to a model, an index is automatically created in foreign_key.

Migration API changed in Rails 5 -

Rails 5 API , - , null: false , null .

, , . , Rails 5 , index: true. , .

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

rails g model Task user:references

Rails 4

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

5

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

foreign_key index ( ).

, , .

.

+4

Source: https://habr.com/ru/post/1656221/


All Articles