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
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With