使用add_reference时指定自定义索引名称 [英] Specify custom index name when using add_reference

查看:120
本文介绍了使用add_reference时指定自定义索引名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行以下迁移

class LinkDoctorsAndSpecializations < ActiveRecord::Migration
  def up
    add_reference :doctors, :doctor_specialization, polymorphic: true, index: true
  end

  def down
    remove_reference :doctors, :doctor_specialization, polymorphic: true
  end
end

当我运行 rake db:migrate 我遇到错误

索引名'表'上的index_doctors_on_doctor_specialization_type_and_doctor_specialization_id'医生的时间太长;限制为63个字符

所以在使用add_reference时(如我们在中指定的方式),如何指定索引名称add_index:table,:column,:name => '索引名称'

so how can i specify the index name when using add_reference like the way we specify in add_index :table, :column, :name => 'index name'

推荐答案

评论时,这样做:

add_index :table, :column, name: 'index name' 

这里是文档
或者,您可以尝试以下操作:

Here is documentation. Or, you can try this :

class LinkDoctorsAndSpecializations < ActiveRecord::Migration
  def change
    add_reference :doctors, :doctor_specialization, polymorphic: true, index: { name: 'index name' }
  end
end

这篇关于使用add_reference时指定自定义索引名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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