在小数列中设置默认值 [英] Set a default value in a decimal column

查看:52
本文介绍了在小数列中设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Rails 的小数列中设置默认值?我已经尝试了以下两种方法,使用 Rails 3 和 Postgresql,但是在每一种之后,控制台都会告诉我默认值仍然是 nil.如果我从控制台设置该值,则没有问题,但它似乎在迁移中不起作用.

How do you set a default value in a decimal column in Rails? I've tried both the following, using Rails 3 and Postgresql, but after each one the console tells me the default values are still nil. If I set the value from the console, there's no problem, but it doesn't seem to work in the migration.

#Attempt 1
add_column :providers, :commission, :decimal, :precision=>6,:scale=>4,:default=>0.1

#Attempt 2
add_column :providers, :commission, :decimal, :precision=>6,:scale=>4,:default=>BigDecimal("0.1")

非常感谢您的帮助!

推荐答案

原来我还需要设置 :null=>false

It turns out I also need to set :null=>false

以下代码有效:

add_column :providers, :commission, :decimal, :precision=>6,:scale=>4,:default=>0.1, :null => false

这篇关于在小数列中设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆