Rails ActiveRecord::Migration index: true 和 add_index 有什么区别? [英] Rails ActiveRecord::Migration what is the difference between index: true and add_index?

查看:14
本文介绍了Rails ActiveRecord::Migration index: true 和 add_index 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别

t.boolean :is_live, index: true

add_index :table_name, :is_live

如果没有区别,为什么只有add_index反映在schema.rb中.当我使用 index: true 时,我实际上看不到 schema.rb 中的索引.我应该只使用 add_index 方法.

If there is no difference, how come only the add_index is reflected in schema.rb. When I use index: true, I can't actually see the index in schema.rb. Should I only use the add_index method.

当使用 add_index 方法时,我可以在我的 schema.rb 中看到这个

When use the add_index method, I can see this in my schema.rb

add_index "table_name", ["is_live"], name: "index_table_name_on_is_live", using: :blahblah

推荐答案

简而言之:两者都做同样的工作.ìindex: true` 只是为你节省了额外的一行.看这里 https://github.com/rails/rails/pull/5262#issuecomment-4329571

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

这篇关于Rails ActiveRecord::Migration index: true 和 add_index 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆