使用 add_reference 时指定自定义索引名称
Specify custom index name when using 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 name 'index_doctors_on_doctor_specialization_type_and_doctor_specialization_id' on table 'doctors' is too long; the limit is 63 characters
所以我如何在使用 add_reference 时指定索引名称,就像我们在 add_index :table, :column, :name => 'index name'
中指定的那样
这确实可行:
add_index :table, :column, name: 'index name'
Take a look 更多示例。
我听说解决此问题的最佳方法是将其从添加参考行中删除并在下面手动指定它,就像您问题的最后一行一样
add_index :table, :column, :name => 'index name'
像我一样:
add_index :table, :column, name: 'index name'
这里是documentation。
或者,您可以试试这个:
class LinkDoctorsAndSpecializations < ActiveRecord::Migration
def change
add_reference :doctors, :doctor_specialization, polymorphic: true, index: { name: 'index name' }
end
end
我不建议省略 "add_reference",但您可以省略 "index" 选项哈希键,然后使用 "add_index":
add_reference :table, :column
add_index :table, :column, :name => 'index_table_column'
或者,更合适的方式是这样的:
add_reference :doctors, :doctor_specialization, polymorphic: true, index: { name: 'index_doctors_doctor_specialization' }
我有以下迁移
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 name 'index_doctors_on_doctor_specialization_type_and_doctor_specialization_id' on table 'doctors' is too long; the limit is 63 characters
所以我如何在使用 add_reference 时指定索引名称,就像我们在 add_index :table, :column, :name => 'index name'
这确实可行:
add_index :table, :column, name: 'index name'
Take a look 更多示例。
我听说解决此问题的最佳方法是将其从添加参考行中删除并在下面手动指定它,就像您问题的最后一行一样
add_index :table, :column, :name => 'index name'
像我
add_index :table, :column, name: 'index name'
这里是documentation。 或者,您可以试试这个:
class LinkDoctorsAndSpecializations < ActiveRecord::Migration
def change
add_reference :doctors, :doctor_specialization, polymorphic: true, index: { name: 'index name' }
end
end
我不建议省略 "add_reference",但您可以省略 "index" 选项哈希键,然后使用 "add_index":
add_reference :table, :column
add_index :table, :column, :name => 'index_table_column'
或者,更合适的方式是这样的:
add_reference :doctors, :doctor_specialization, polymorphic: true, index: { name: 'index_doctors_doctor_specialization' }