postgresql 如何创建 Rails 迁移以删除/更改精度和小数位数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13115772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:28:55  来源:igfitidea点击:

How do I create a rails migration to remove/change precision and scale on decimal?

ruby-on-railsdatabaseruby-on-rails-3postgresql

提问by Richard Burton

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

我正在尝试从NUMERIC我的数据库中的小数 (PostgreSQL ) 字段中删除精度和比例属性?

The fields:

田野:

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

Here's the context: "PG::Error - numeric field overflow" on Heroku

这是上下文:Heroku 上的“PG::Error - numeric field overflow”

回答by Dipak Panchal

format :

格式 :

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

First in you terminal:

首先在你的终端:

rails g migration change_numeric_field_in_my_table

Then in your migration file:

然后在您的迁移文件中:

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

then

然后

run rake db:migrate

Source : http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

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

回答by jous32

In your migration file change your field to :integer and run run rake db:migrate

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