Ruby-on-rails Rails:添加列后添加索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15881749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: Adding an index after adding column
提问by user1611830
Suppose I created a table tablein a Rails app. Some time later, I add a column running:
假设我table在 Rails 应用程序中创建了一个表。一段时间后,我添加一列运行:
rails generate migration AddUser_idColumnToTable user_id:string.
Then I realize I need to add user_idas an index. I know about the add_indexmethod, but where should this method be called? Am I supposed to run a migration (if yes, which one ?), then adding by hand this method?
然后我意识到我需要添加user_id为索引。我知道这个add_index方法,但是这个方法应该在哪里调用?我是否应该运行迁移(如果是,是哪个?),然后手动添加此方法?
回答by Jaap Haagmans
You can run another migration, just for the index:
您可以运行另一个迁移,仅用于索引:
class AddIndexToTable < ActiveRecord::Migration
def change
add_index :table, :user_id
end
end
回答by Vadym Tyemirov
If you need to create a user_idthen it would be a reasonable assumption that you are referencing a user table. In which case the migration shall be:
如果您需要创建一个,user_id那么可以合理地假设您正在引用一个用户表。在这种情况下,迁移应为:
rails generate migration AddUserRefToProducts user:references
This command will generate the following migration:
此命令将生成以下迁移:
class AddUserRefToProducts < ActiveRecord::Migration
def change
add_reference :user, :product, index: true
end
end
After running rake db:migrateboth a user_idcolumn and an index will be added to the productstable.
运行后rake db:migrate既user_id列和索引将被添加到products表中。
In case you just need to add an index to an existing column, e.g. nameof a usertable, the following technique may be helpful:
如果你只需要一个索引添加到现有的塔,例如name一的user表,下面的技术可能会有所帮助:
rails generate migration AddIndexToUsers name:string:indexwill generate the following migration:
rails generate migration AddIndexToUsers name:string:index将生成以下迁移:
class AddIndexToUsers < ActiveRecord::Migration
def change
add_column :users, :name, :string
add_index :users, :name
end
end
Delete add_columnline and run the migration.
删除add_column行并运行迁移。
In the case described you could have issued rails generate migration AddIndexIdToTable index_id:integer:indexcommand and then delete add_columnline from the generated migration. But I'd rather recommended to undo the initial migration and add reference instead:
在所描述的情况下,您可以发出rails generate migration AddIndexIdToTable index_id:integer:index命令,然后add_column从生成的迁移中删除行。但我宁愿建议撤消初始迁移并添加引用:
rails generate migration RemoveUserIdFromProducts user_id:integer
rails generate migration AddUserRefToProducts user:references
回答by rdeandrade
Add in the generated migration after creating the column the following (example)
在创建列后在生成的迁移中添加以下内容(示例)
add_index :photographers, :email, :unique => true
回答by Mauro
For references you can call
如需参考,您可以致电
rails generate migration AddUserIdColumnToTable user:references
If in the future you need to add a general index you can launch this
如果将来您需要添加通用索引,您可以启动它
rails g migration AddOrdinationNumberToTable ordination_number:integer:index
Generate code:
生成代码:
class AddOrdinationNumberToTable < ActiveRecord::Migration
def change
add_column :tables, :ordination_number, :integer
add_index :tables, :ordination_number, unique: true
end
end
回答by vidur punj
You can use this, just think Job is the name of the model to which you are adding index cader_id:
您可以使用它,只需认为 Job 是您添加索引cader_id的模型的名称:
class AddCaderIdToJob < ActiveRecord::Migration[5.2]
def change
change_table :jobs do |t|
t.integer :cader_id
t.index :cader_id
end
end
end

