Change database column type to decimal

笑着哭i 提交于 2020-06-29 05:59:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!