【问题标题】:Rails, Postgres - Change column type from integer to stringRails,Postgres - 将列类型从整数更改为字符串
【发布时间】:2023-03-20 04:35:01
【问题描述】:

我有一个 Rails 3.2 应用程序,它将邮政编码存储在 user_profiles 表中。我最初使用整数作为邮政编码的数据类型;但是,这显然是错误的,因为您不能使用以零开头的数字(并且某些美国邮政编码确实以零开头)。因此,我需要将邮政编码列和我的生产数据转换为字符串。我认为这是通过简单的 change_column 迁移完成的,但我不知道使用 Postgres 时要使用的确切代码。任何帮助将非常感激!我添加邮政编码列的原始迁移如下:

class AddZipCodeToUserProfiles < ActiveRecord::Migration
  def change
    add_column :user_profiles, :zip_code, :integer
  end
end

【问题讨论】:

    标签: ruby-on-rails postgresql


    【解决方案1】:
    class ChangeZipCodeToString < ActiveRecord::Migration
      def change
        change_column :user_profiles, :zip_code, :string
      end
    end
    

    你试过了吗?

    【讨论】:

    • 谢谢你 joshua.paling!工作完美。我期待来自 Postgres 的某种错误消息,但它没有出现任何错误。干杯。
    猜你喜欢
    • 2020-11-10
    • 2015-08-02
    • 2012-05-07
    • 2015-12-28
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多