如何创建rails迁移以删除/更改精度并按小数缩放?

时间:2012-10-29 04:13:18

标签: ruby-on-rails database ruby-on-rails-3 postgresql

我正在尝试从数据库中的十进制(PostgreSQL NUMERIC)字段中删除precision和scale属性?

字段:

t.decimal  "revenue_per_transaction", :precision => 8, :scale => 2
t.decimal  "item_quantity",           :precision => 8, :scale => 2
t.decimal  "goal_conversion",         :precision => 8, :scale => 2
t.decimal  "goal_abandon",            :precision => 8, :scale => 2
t.decimal  "revenue",                 :precision => 8, :scale => 2

我需要添加哪些内容才能将这些更改为无限制的比例和精度,或者增加比例?目前我正在达到规模限制并出现错误,如:

ERROR:   numeric field overflow

以下是上下文:"PG::Error - numeric field overflow" on Heroku

2 个答案:

答案 0 :(得分:60)

格式:

change_column(table_name, column_name, type, options): Changes the column to a different type using the same parameters as add_column.

首先在你的终端:

rails g migration change_numeric_field_in_my_table

然后在您的迁移文件中:

class ChangeNumbericFieldInMyTable < ActiveRecord::Migration
  def self.up
   change_column :my_table, :revenue_per_transaction, :decimal, :precision => give whatever, :scale => give whatever
  end
end

然后

run rake db:migrate

来源:http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

答案 1 :(得分:0)

在迁移文件中,将字段更改为:integer 并运行 运行rake db:migrate