将数据库列类型更改为十进制 [英] Change database column type to decimal
本文介绍了将数据库列类型更改为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将Rails 3.2.3和MySQL用于我的数据库
我创建了一个名为affiliate_payment.rb的模型,其列为:amount
,最初的数据类型为 float
。我尝试通过以下迁移将其更改为十进制:
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:迁移
...
但是当我检查列类型进行确认时,我发现列类型现在为整数
!
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?
推荐答案
尝试一下:
change_column :affiliate_payments, :amount, :decimal, :precision => 8, :scale => 2
这将成为 BigDecimal
这篇关于将数据库列类型更改为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文