Ruby-on-rails 更改列名 Rails
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34796492/
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
change column name Rails
提问by Pere
I have this table:
我有这张桌子:
class CreateShoes < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.string :name
t.boolean :leather
t.integer :season
t.timestamps null: false
end
end
end
the 'season' column should be called 'season_id'. I know that I have to write 't.rename :season, :season_id' as explained in http://edgeguides.rubyonrails.org/active_record_migrations.html#column-modifiersbut I don't manage to find the right syntax. Should it be?
“季节”列应称为“季节 ID”。我知道我必须按照http://edgeguides.rubyonrails.org/active_record_migrations.html#column-modifiers 中的说明编写 't.rename :season, :season_id'但我找不到正确的语法。应该是吗?
class CreateShoes < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.string :name
t.boolean :leather
t.integer :season
t.timestamps null: false
end
change_table :products do |t|
t.rename :season, :season_id
end
end
end
Doesn't work. Anything I have to do in the Mac console? Thanks!
不起作用。我必须在 Mac 控制台中做些什么?谢谢!
回答by Inpego
Run in your console:
在您的控制台中运行:
$ rails g migration rename_season_to_season_id
Now file db/migrate/TIMESTAMP_rename_season_to_season_id.rbcontains following:
现在文件db/migrate/TIMESTAMP_rename_season_to_season_id.rb包含以下内容:
class RenameSeasonToSeasonId < ActiveRecord::Migration
def change
end
end
Modify it as follows:
修改如下:
class RenameSeasonToSeasonId < ActiveRecord::Migration
def change
rename_column :shoes, :season, :season_id
end
end
Then run $ rake db:migratein console.
然后$ rake db:migrate在控制台运行。
回答by ruby_newbie
Either fix your migration and do
要么修复你的迁移,然后做
rake db:rollback db:migrate
or make another migration like so:
或者像这样进行另一次迁移:
rename_column :shoes, :season, :season_id if column_exists?(:shoes, :season) && !column_exists?(:shoes, :season_id)
and then do
然后做
rake db:migrate
回答by Sushant
If your branch has been pushed to production then one probably needs to add the new migration by running the following command:
如果您的分支已推送到生产,那么您可能需要通过运行以下命令来添加新的迁移:
rails g migration RenameSeasonColumnNameToShoes
rails g migration RenameSeasonColumnNameToShoes
and if it hasn't been pushed to production or you have to currently make changes to your branch only, then do:
如果它还没有被推送到生产环境或者你目前只需要对你的分支进行更改,那么执行以下操作:
bundle exec rake db:rollback
bundle exec rake db:rollback
Then make changes to your migration file inside /db/migrate/<your_migration_file_name>
然后在里面修改你的迁移文件 /db/migrate/<your_migration_file_name>
Then in your migration file, using rename_columndo as follows:
然后在您的迁移文件中,使用rename_columndo 如下:
class RenameSeasonColumnNameToShoes < ActiveRecord::Migration
def change
rename_column :shoes, :season, :season_id
end
end
and then do
然后做
bundle exec rake db:migrate db:test:prepare
bundle exec rake db:migrate db:test:prepare
回答by dixpac
If your intention is to rename column in table than you example migration is not making sense :)... Instead of this
如果您打算重命名表中的列,那么示例迁移没有意义:)... 而不是这个
class CreateShoes < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.string :name
t.boolean :leather
t.integer :season
t.timestamps null: false
end
change_table :products do |t|
t.rename :season, :season_id
end
end
end
You just need table change migration, like this(Rails will take care rollback for your):
您只需要表更改迁移,就像这样(Rails 会为您处理回滚):
class RenameSessionColumnInsideShoes < ActiveRecord::Migration
def change
change_table :products do |t|
t.rename :season, :season_id
end
end
end
renamemethod on table object in rails is valid method, as you can see in Rails source code
renamerails 中 table 对象的方法是有效的方法,如您在 Rails 源代码中所见

