Ruby on rails 如何更改已使用rails迁移创建的表的索引?
我已经创建了这个迁移文件,并且迁移已经运行Ruby on rails 如何更改已使用rails迁移创建的表的索引?,ruby-on-rails,ruby,ruby-on-rails-4,rails-activerecord,rails-migrations,Ruby On Rails,Ruby,Ruby On Rails 4,Rails Activerecord,Rails Migrations,我已经创建了这个迁移文件,并且迁移已经运行 class CreateStudentContexts < ActiveRecord::Migration def change create_table :student_contexts do |t| t.string :student_id, index: true, null: false, limit: 40 t.text :data t.timestamps null: false
class CreateStudentContexts < ActiveRecord::Migration
def change
create_table :student_contexts do |t|
t.string :student_id, index: true, null: false, limit: 40
t.text :data
t.timestamps null: false
t.index [:student_id, :updated_at], unique: true, name: 'student_context_index'
end
end
end
现在我想创建一个新的迁移文件来更新索引,并且索引字段应该是student\u id,并在创建时创建。您可以创建另一个迁移并更改表。差不多
class MyMigration < ActiveRecord::Migration
def change
remove_index :student_contexts, :updated_at
add_index :student_contexts, :created_at
end
end
可以。您可以创建另一个迁移并更改表。差不多
class MyMigration < ActiveRecord::Migration
def change
remove_index :student_contexts, :updated_at
add_index :student_contexts, :created_at
end
end
可以。这个问题有点老了,但万一有人碰到它: Justin Wood的答案应该有效,但有一个变化:用索引名称替换remove_index方法中的列名:
remove_index :student_contexts, name: 'student_context_index'
创建索引时,会使用name参数为其指定一个特定的非标准名称,因此您还需要在remove_index语句中使用name参数。否则,Rails希望索引有一个标准名称,在本例中,索引为“学生”\u“上下文”\u on\u updated\u at.这个问题有点老了,但万一有人遇到它: Justin Wood的答案应该有效,但有一个变化:用索引名称替换remove_index方法中的列名:
remove_index :student_contexts, name: 'student_context_index'
创建索引时,会使用name参数为其指定一个特定的非标准名称,因此您还需要在remove_index语句中使用name参数。否则,Rails希望索引有一个标准名称,在本例中,index\u student\u contexts\u on\u updated\u at.它抛出一个错误,“index\u name\u for\u remove”:发生了一个错误,此迁移和所有后续迁移已取消:StandardError Index name“Index_student_contexts_on_updated_at”on table“student_contexts”不存在引发“Index_name_for_remove”错误:发生错误,此迁移和所有后续迁移已取消:StandardError索引名称“Index_student_contexts_on_updated_at”on table“student_contexts”不存在