Rails ActiveRecord::Migration what is the difference between index: true and add_index?

前端 未结 2 742
独厮守ぢ
独厮守ぢ 2021-02-07 01:36

What is the difference between

t.boolean :is_live, index: true

and

add_index :table_name, :is_live

If there i

相关标签:
2条回答
  • 2021-02-07 01:53

    In short: both do the same job. ìndex: true` just saves you an additional line. Look here https://github.com/rails/rails/pull/5262#issuecomment-4329571

    0 讨论(0)
  • 2021-02-07 01:56

    Just found out that the :index option is for references only (t.references or t.belongs_to). For 'ordinary' column types this option is ignored (that's why indices are not reflected in your schema.db when you used the :index option).

    For less verbose syntax there is an index type:

    t.index :column_name # extra options may be provided also
    
    0 讨论(0)
提交回复
热议问题