Rails:如何创建将“using :: btree”添加到外键的迁移

时间:2016-12-06 10:11:05

标签: ruby-on-rails

我有两个型号 父亲有很多儿子 儿子属于父亲

通常,这段代码

def change
    add_reference :sons, :father, index: true
end

它会生成

add_index "sons", ["father_id"], name: "index_sons_on_father_id"

现在,我希望它生成

add_index "sons", ["father_id"], name: "index_sons_on_father_id", using: :btree

如何编写迁移?

1 个答案:

答案 0 :(得分:0)

您只需查看this

即可

你可以这样做。

ActiveRecord::Schema.define(version: 20150321171548) do
  create_table "authors", force: :cascade do |t|
    t.string   "name"
    t.string   "hashtags"
    t.string   "avatar"
    t.string   "email"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "articles", force: :cascade do |t|
    t.integer  "author_id",    limit: 4
    t.string   "title",        limit: 255
    t.string   "bannerurl",    limit: 255
    t.string   "thumbnailurl", limit: 255
    t.text     "content",      limit: 65535
    t.datetime "created_at",                 null: false
    t.datetime "updated_at",                 null: false
  end

  add_index "articles", ["author_id"], name: "index_articles_on_author_id", using: :btree
end