Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

like image 227
Richard Burton Avatar asked Oct 29 '12 04:10

Richard Burton


People also ask

How do I migrate a specific migration in Rails?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.

What does db Migrate do Rails?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.

How do I Downgrad in Rails?

You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.

Can you edit a migration file Rails?

If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run rake db:migrate.


1 Answers

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

like image 107
Dipak Panchal Avatar answered Oct 06 '22 01:10

Dipak Panchal