【问题标题】:Does the `index` option on Rails column migrations work?Rails 列迁移上的 `index` 选项是否有效?
【发布时间】:2015-03-27 19:23:14
【问题描述】:

我可以编写如下迁移:

class CreateFoos < ActiveRecord::Migration
  def change
    create_table :foos do |t|
      t.string :bar, index: true
    end
  end
end

根据docs index: true 也应该为该列上的表创建一个索引,但是我可以在schema.rb 中看到它没有。

这是一个已知错误还是与我的设置有关? FWIW 我在 Rails 4.1.8 上使用 Postgres 和 PostGIS。

这也不适用于 t.column :bar, :string, index: true 的较长语法,尽管它似乎适用于 t.belongs_to。

【问题讨论】:

  • 我很确定它只适用于数字,因为索引是为了加快关联检索。
  • 您的确切代码在 Rails 4.2 中适用于我,db/schema.rb 最终得到:add_index "foos", ["bar"], name: "index_foos_on_bar"。
  • 你是对的,它在 4.1.8 中不起作用

标签: ruby-on-rails postgresql activerecord rails-migrations


【解决方案1】:

所以,确认后,这在 4.1.8 和 4.1.9 中都不起作用,是的,这是一个错误。

在 4.2.0 中修复了该错误(并且 4.1 -> 4.2 升级很简单)。

【讨论】:

  • 感谢您的挖掘!
猜你喜欢
  • 2013-07-17
  • 2015-09-17
  • 1970-01-01
  • 2015-10-02
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2014-05-22
相关资源
最近更新 更多