【问题标题】:Convert SQLite String to PostgreSQL将 SQLite 字符串转换为 PostgreSQL
【发布时间】:2014-01-14 19:48:55
【问题描述】:

我最近在 Rails 中使用 migrate 将字符串转换为小数,这在本地运行良好。然后我将我的应用程序推送到 heroku 并收到以下错误:

Running `rake db:migrate` attached to terminal... up, run.9099
==  ChangePriceToDecimal: migrating ===========================================
-- change_column(:products, :price, :decimal, {:precision=>8, :scale=>2})
rake aborted!
An error has occurred, this and all later migrations canceled:

PG::Error: ERROR:  column "price" cannot be cast automatically to type numeric
HINT:  Specify a USING expression to perform the conversion.
: ALTER TABLE "products" ALTER COLUMN "price" TYPE decimal(8,2)

这是我使用的迁移文件:

class ChangePriceToDecimal < ActiveRecord::Migration
def change
change_column :products, :price, :decimal, precision: 8, scale: 2
end 
end

知道我能做些什么来解决这个问题吗?

谢谢, T

【问题讨论】:

标签: ruby-on-rails postgresql ruby-on-rails-4


【解决方案1】:

你有两种可能:

  1. 在 Ruby 中执行转换。 在这种情况下,创建另一个列,例如所需格式的temporary_price。使用 rake 任务将数据从 price 复制到 temporary_price 进行相应的转换。完成后,删除 price 并将 temporary_price 重命名为 price 并再次迁移。

  2. 使用 SQL 执行转换。请参阅以下答案/链接:

【讨论】:

    猜你喜欢
    • 2011-05-24
    • 1970-01-01
    • 2017-10-03
    • 2014-02-26
    • 2020-09-20
    • 1970-01-01
    • 2021-04-02
    • 2020-05-31
    • 1970-01-01
    相关资源
    最近更新 更多