Derek Derek - 5 months ago 35
MySQL Question

Is activerecord truncating timestamp precision?

Using Rails 4.0.0, ruby 2.0p247 with mysql2 0.3.13 gem. A column in my table in MariaDB 5.5

Note: mariadb has had microsecond precision support since 5.3

`updated_at` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,


The precision should be microseconds so I tried in my model file

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


But in the database it is still

| updated_at |
+----------------------------+
| 2013-10-03 19:35:00.000000 |
+----------------------------+


Why is rails/activerecord not storing microseconds?

Answer

Got it to work:

added this to a file in config/initializers

class Time
  DATE_FORMATS = {
    :db => '%Y-%m-%d %H:%M:%S.%6N'
  }
end

see conversions.rb for more info.

Comments