mysql-activerecord截断时间戳精度吗?
作者:互联网
使用Rails 4.0.0,Ruby 2.0p247和mysql2 0.3.13 gem. MariaDB 5.5中我的表中的一列
注意:mariadb自5.3起提供微秒精度支持
`updated_at` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
精度应该是微秒,所以我在模型文件中尝试了
before_save :set_update
def set_update
# also tried
# self.updated_at = "2013-10-03 12:35:00.123456 -700"
# self.updated_at = Time.now.strftime "%Y-%m-%d %H:%M:%S %6N"
# self.updated_at = Time.now.iso8601(6)
# doesn't work, still no microseconds
self.updated_at = Time.now.to_f
end
但是在数据库中它仍然
| updated_at |
+----------------------------+
| 2013-10-03 19:35:00.000000 |
+----------------------------+
为什么rails / activerecord不存储微秒?
解决方法:
得到它的工作:
将此添加到config / initializers中的文件中
class Time
DATE_FORMATS = {
:db => '%Y-%m-%d %H:%M:%S.%6N'
}
end
有关更多信息,请参见conversions.rb.
标签:activerecord,ruby-on-rails-4,ruby,ruby-on-rails,mysql 来源: https://codeday.me/bug/20191122/2061675.html