如何创建rails迁移以删除/更改精度和十进制缩放? [英] How do I create a rails migration to remove/change precision and scale on decimal?

查看:368
本文介绍了如何创建rails迁移以删除/更改精度和十进制缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中的十进制(PostgreSQL NUMERIC )字段中删除精度和缩放属性?

I am trying to remove the precision and scale attributes from decimal (PostgreSQL NUMERIC) fields in my database?

字段:

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

我需要添加到迁移才能将这些更改为无界的缩放和精度,增加规模?目前我遇到的规模限制,并得到错误像:

What do I need to add to my migration to change these to unbounded scale and precision, or to increase the scale? At the moment I'm hitting the scale limit and getting errors like:

ERROR:   numeric field overflow

这里是上下文:PG ::错误 - 数字字段溢出在Heroku上

推荐答案

格式:

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

这篇关于如何创建rails迁移以删除/更改精度和十进制缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆