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
How do I create a rails migration to remove/change precision and scale on decimal?
提问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
回答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