我错误地将列命名为hased_password而不是hashed_password。

如何使用迁移重命名此列来更新数据库架构?


当前回答

我在使用PostgreSQL数据库开发Rails 6应用程序时遇到了这个挑战。

以下是我修复它的方法:

在我的例子中,table_name是“Products”,old_column是“SKU”,new_column则是“ProductNumber”。

创建包含用于重命名列的命令的迁移文件:rails生成迁移重命名KUToProductNumberInProducts打开db/migrate目录中的迁移文件:数据库/migrate/2021028082344_rename_sku_to_product_number_in_products.rb添加用于重命名列的命令:类RenameKuToProductNumberInProducts<ActiveRecord::迁移[6.0]def更改#重命名列:表名,:旧列,:新列重命名列:产品,:sku,:product_number终止终止保存,然后运行迁移命令:轨道数据库:移植

现在,您可以通过查看架构文件来确认列的重命名:

    db/schema.rb

如果您对列的重命名不满意,可以始终回滚:

    rails db:rollback

注意:在调用列名的所有位置,努力将列名修改为新名称。

其他回答

我们可以手动使用以下方法:

我们可以手动编辑迁移,如:

打开app/db/migrate/xxxxx_migration_file.rb将hased_password更新为hashed_passwords运行以下命令$>rakedb:migrate:down VERSION=xxxxxxxxx

然后它将删除您的迁移:

$> rake db:migrate:up VERSION=xxxxxxxxx

它将使用更新的更改添加您的迁移。

RubyonRails的某些版本支持向上/向下迁移方法,如果您的迁移中有向上/下方法,那么:

def up
    rename_column :table_name, :column_old_name, :column_new_name
end

def down
    rename_column :table_name, :column_new_name, :column_old_name
end

如果您在迁移中使用了更改方法,那么:

def change
    rename_column :table_name, :column_old_name, :column_new_name
end

有关更多信息,请参阅:RubyonRails-迁移或活动记录迁移。

让我们接吻。它只需要三个简单的步骤。以下适用于Rails 5.2。

1.创建迁移

rails g迁移重命名为FullNameInStudentsrails g RenameOldFieldToNewFieldInTableName-这样以后代码库的维护人员就非常清楚了(表名使用复数)。

2.编辑迁移

#我更喜欢显式地编写panddown方法。

# ./数据库/migrate/20190114045137_rename_name_to_full_name_in_students.rb

class RenameNameToFullNameInStudents < ActiveRecord::Migration[5.2]
  def up
    # rename_column :table_name, :old_column, :new_column
    rename_column :students, :name, :full_name
  end

  def down
            # Note that the columns are reversed
    rename_column :students, :full_name, :name
  end
end

3.运行迁移

rake数据库:迁移

你要去参加比赛了!

如果该列中已经填充了数据并在生产环境中运行,我建议采用一步一步的方法,以便在等待迁移时避免生产环境中的停机。

首先,我将创建一个数据库迁移,以添加具有新名称的列,并用旧列名的值填充它们。

class AddCorrectColumnNames < ActiveRecord::Migration
  def up
    add_column :table, :correct_name_column_one, :string
    add_column :table, :correct_name_column_two, :string

    puts 'Updating correctly named columns'
    execute "UPDATE table_name SET correct_name_column_one = old_name_column_one, correct_name_column_two = old_name_column_two"
    end
  end

  def down
    remove_column :table, :correct_name_column_one
    remove_column :table, :correct_name_column_two
  end
end

然后我会做出改变,并将其付诸生产。

git commit -m 'adding columns with correct name'

然后,一旦提交被推到生产中,我就会运行。

Production $ bundle exec rake db:migrate

然后,我会将引用旧列名的所有视图/控制器更新为新列名。运行我的测试套件,并提交这些更改。(确保它在本地工作并首先通过所有测试后!)

git commit -m 'using correct column name instead of old stinky bad column name'

然后我会把这个承诺推向生产。

此时,您可以删除原始列,而不必担心与迁移本身相关的任何停机时间。

class RemoveBadColumnNames < ActiveRecord::Migration
  def up
    remove_column :table, :old_name_column_one
    remove_column :table, :old_name_column_two
  end

  def down
    add_column :table, :old_name_column_one, :string
    add_column :table, :old_name_column_two, :string
  end
end

然后将最新的迁移推送到生产环境,并在后台运行bundle exec rake db:migrate。

我意识到这是一个更复杂的过程,但我宁愿这样做,也不想在生产迁移中遇到问题。

在控制台中:

rails generate migration newMigration

在newMigration文件中:

class FixColumnName < ActiveRecord::Migration
  def change
    rename_column :table_name, :old_column, :new_column
  end
end