问题
I am using Rails 3.2.3, and MySQL for my database
I have created a model affiliate_payment.rb, with a column :amount
, initially with datatype float
. I tried to change this to a decimal with the following migration:
class ChangeAffiliateIdAmountToDecimal < ActiveRecord::Migration
def up
change_column :affiliate_payments, :amount, :decimal
end
def down
change_column :affiliate_payments, :amount, :float
end
end
Ran rake db:migrate
...
But when I check the column type to confirm, I find that the column type is now integer
!
2.0.0-p353 :101 > AffiliatePayment.columns_hash["amount"].type
=> :integer
Can someone explain what I am doing wrong?
回答1:
try this:
change_column :affiliate_payments, :amount, :decimal, :precision => 8, :scale => 2
This will become BigDecimal
回答2:
Use this with new ruby syntax:
change_column :affiliate_payments, :amount, :decimal, precision: 8, scale: 2
回答3:
Hey You have created the migration for change the column type so rake db:migrate
need not be run.
You have to run the migration with there migration number like below
rake db:migrate:up VERSION = version number of migration.
来源:https://stackoverflow.com/questions/21699047/change-database-column-type-to-decimal