我错误地将列命名为hased_password而不是hashed_password。
如何使用迁移重命名此列来更新数据库架构?
我错误地将列命名为hased_password而不是hashed_password。
如何使用迁移重命名此列来更新数据库架构?
当前回答
def change
rename_column :table_name, :old_column_name, :new_column_name
end
其他回答
打开RubyonRails控制台并输入:
ActiveRecord::Migration.rename_column :tablename, :old_column, :new_column
在我看来,在这种情况下,最好使用rakedb:rollback,然后编辑迁移并再次运行rakedb:migrate。
但是,如果列中有不希望丢失的数据,请使用rename_column。
若您的代码并没有和其他代码共享,那个么最好的选择就是只执行rakedb:rollback然后在migration和rakedb:migrate中编辑列名。就是这样
您可以编写另一个迁移来重命名列
def change
rename_column :table_name, :old_name, :new_name
end
就是这样。
create_table的近亲是change_table,用于更改现有表。它以与create_table类似的方式使用,但向块提供的对象知道更多技巧。例如:
class ChangeBadColumnNames < ActiveRecord::Migration
def change
change_table :your_table_name do |t|
t.rename :old_column_name, :new_column_name
end
end
end
如果我们将其与其他alter方法一起使用,例如:remove/add index/remove index/add column,则这种方法更有效。我们可以做以下事情:
重命名
t.rename :old_column_name, :new_column_name
添加列
t.string :new_column
删除列
t.remove :removing_column
索引列
t.index :indexing_column
我们可以手动使用以下方法:
我们可以手动编辑迁移,如:
打开app/db/migrate/xxxxx_migration_file.rb将hased_password更新为hashed_passwords运行以下命令$>rakedb:migrate:down VERSION=xxxxxxxxx
然后它将删除您的迁移:
$> rake db:migrate:up VERSION=xxxxxxxxx
它将使用更新的更改添加您的迁移。