【问题标题】:how to write a migration to alter a table column from float to bigint in Rails如何编写迁移以在 Rails 中将表列从 float 更改为 bigint
【发布时间】:2012-01-20 03:53:40
【问题描述】:

我在用户表中有数据。 一列称为:provider_user_id,数据类型为 float。

我想把它改成数据类型 bigint。

我应该如何在 rails 3 中编写此迁移

原始列是通过以下迁移创建的:

class AddFbuidToUsers < ActiveRecord::Migration
  def self.up
    add_column :users, :provider_user_id, :float
  end

  def self.down
    remove_column :users, :provider_user_id
  end
end

【问题讨论】:

标签: ruby-on-rails-3 migration


【解决方案1】:
change_column :users, :provider_user_id, :bigint

【讨论】:

    【解决方案2】:

    添加一个 bigint 类型的新列,任何名称都可以。

    编写一个更新循环以使用当前列中的值更新每个新的 bigint 列

    删除旧列(希望上面没有 RI)。

    将新列重命名为刚刚删除的旧列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2020-07-30
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 1970-01-01
      相关资源
      最近更新 更多