我错误地将列命名为hased_password而不是hashed_password。
如何使用迁移重命名此列来更新数据库架构?
我错误地将列命名为hased_password而不是hashed_password。
如何使用迁移重命名此列来更新数据库架构?
当前回答
这可能比重命名列、创建新列并复制内容更好:
通过这种方式,我们可以保存旧列中的内容
这可能是一代人:
rails generate migration add_birthdate_to_User birthdate:string
这可能是迁移:
class AddBirthdateToUser < ActiveRecord::Migration[7.0]
def change
add_column :user, :birthdate, :json, default: '[]', null: false
reversible do |dir|
dir.up do
User.update_all('birthdate=birtdate') # rubocop:disable Rails/SkipsModelValidations
end
end
end
end
之后,您必须删除错误的“出生日期”列
class RemoveBirthdateFromUser < ActiveRecord::Migration[7.0]
def change
remove_column :User, :Birtdate, :json
end
end
其他回答
打开RubyonRails控制台并输入:
ActiveRecord::Migration.rename_column :tablename, :old_column, :new_column
我们可以手动使用以下方法:
我们可以手动编辑迁移,如:
打开app/db/migrate/xxxxx_migration_file.rb将hased_password更新为hashed_passwords运行以下命令$>rakedb:migrate:down VERSION=xxxxxxxxx
然后它将删除您的迁移:
$> rake db:migrate:up VERSION=xxxxxxxxx
它将使用更新的更改添加您的迁移。
这可能比重命名列、创建新列并复制内容更好:
通过这种方式,我们可以保存旧列中的内容
这可能是一代人:
rails generate migration add_birthdate_to_User birthdate:string
这可能是迁移:
class AddBirthdateToUser < ActiveRecord::Migration[7.0]
def change
add_column :user, :birthdate, :json, default: '[]', null: false
reversible do |dir|
dir.up do
User.update_all('birthdate=birtdate') # rubocop:disable Rails/SkipsModelValidations
end
end
end
end
之后,您必须删除错误的“出生日期”列
class RemoveBirthdateFromUser < ActiveRecord::Migration[7.0]
def change
remove_column :User, :Birtdate, :json
end
end
首先你需要跑步
rails g migration create_new_column_in_tablename new_column:datatype
rails g migration remove_column_in_tablename old_column:datatype
然后需要检查db/migration您可以检查nem迁移中的详细信息,如果所有详细信息都正确,则需要运行:
rails db:migrate
$: rails g migration RenameHashedPasswordColumn
invoke active_record
create db/migrate/20160323054656_rename_hashed_password_column.rb
打开该迁移文件并按如下方式修改该文件(请输入原始table_name)
class RenameHashedPasswordColumn < ActiveRecord::Migration
def change
rename_column :table_name, :hased_password, :hashed_password
end
end