Rails' datetime and timestamp in a migration file are the same for MySQL and Sqlite3?

14,770

I would recommend using the :datetime, because so it's clear what to use. I believe rails is using DATETIME for both in the DB is because of the Problem that the datetimes representable with the unix timestamp or the MySQL TIMESTAMP field. Since timestamp is by default a 32bit Integer (see Wikipedia:Timestamp) it can only represent dates between 1901-12-13 (or 1970-01-01 if no negative values are allowed like in MySQL) and 2038-01-19. After or before that it will overflow. This is the year 2038 problem.

So to make it clear to everybody I would name it :datetime in the migration.

The TIMESTAMP data type has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. It has varying properties, depending on the MySQL version and the SQL mode the server is running in. These properties are described later in this section. Source

Share:
14,770
nonopolarity
Author by

nonopolarity

I started with Apple Basic and 6502 machine code and Assembly, then went onto Fortran, Pascal, C, Lisp (Scheme), microcode, Perl, Java, JavaScript, Python, Ruby, PHP, and Objective-C. Originally, I was going to go with an Atari... but it was a big expense for my family... and after months of me nagging, my dad agreed to buy an Apple ][. At that time, the Pineapple was also available. The few months in childhood seem to last forever. A few months nowadays seem to pass like days. Those days, a computer had 16kb or 48kb of RAM. Today, the computer has 16GB. So it is in fact a million times. If you know what D5 AA 96 means, we belong to the same era.

Updated on July 18, 2022

Comments

  • nonopolarity
    nonopolarity almost 2 years

    :datetime and :timestamp in a migration file seems like the same in MySQL and Sqlite3, and they both map to datetime in the database side, except I can't find that in a formal documentation.

    Also, what about when if our Rails project may use other DBMS, then should we use :datetime or :timestamp when we script/generate (or rails generate) our Model or Scaffold?

  • Radix
    Radix about 8 years
    Thanks @jigfox for this useful information. I'm wondering to know that Rails uses created_at and updated_at as timestamp. Shouldn't it be datetime as timestamp will create problem after 19th Jan, 2038?