Mysql Rails更改\u列从架构中删除索引
我试图使某些列不能为null。我使用Mysql Rails更改\u列从架构中删除索引,mysql,ruby-on-rails,ruby-on-rails-3.2,rails-migrations,Mysql,Ruby On Rails,Ruby On Rails 3.2,Rails Migrations,我试图使某些列不能为null。我使用change\u列没问题,但其中一些列附带了一个索引,表示它是唯一的 但是,当我运行此迁移时: change_column :users, :username, :string, null: false change_column :users, :email, :string, null: false change_column :users, :password, :string, null: false change_column :users, :te
change\u列
没问题,但其中一些列附带了一个索引,表示它是唯一的
但是,当我运行此迁移时:
change_column :users, :username, :string, null: false
change_column :users, :email, :string, null: false
change_column :users, :password, :string, null: false
change_column :users, :terms_agreed, :boolean, null: false
它删除模式中的add\u索引
schema.rb
- add_index "users", ["username"], :name => "index_users_on_username_code", :unique => true
- add_index "users", ["email"], :name => "index_users_on_email_code", :unique => true
add_index "users", ["confirmation_code"], :name => "index_users_on_confirmation_code", :unique => true
如何在不删除索引的情况下执行此操作
另外,它实际上并没有删除数据库中的索引。仅在
schema.rb
文件中。如何处理迁移的行为取决于您的数据库实现。更多信息,但在迁移过程中,您应该在更改中明确请求索引
class DoSomethingToTable < ActiveRecord::Migration
def change
change_column :users, :username, :string, null: false, index: true
end
end
类dosoThingToTable
有关更多信息,请参阅文档。目前还不清楚发生了什么,但事实证明,索引是在某些数据库操作过程中从数据库中删除的
我只是将模式恢复到我需要的状态,然后运行rakedb:setup
,然后再次进行迁移,一切正常。非常混乱,而且花了很多不必要的时间试图弄清楚