Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: why ActiveRecord uses "datetime" type instead of "timestamp" for timestamp fields?

In my database migration file I inserted the line:

t.timestamps

Two columns, as I expected, were created: "updated_at" and "created_at". However, their type is "datetime" and not "timestamp".

I am using MySQL and the "timestamp" type, as I understand, is designed exactly for such cases, as it uses less space and is independent of timezone.

So, is there any reason, why Rails 3 uses "datetime" and not "timestamp"? Should I try to fix that? If yes, is there any way to do this besides not using "t.timestamps" and defining "updated_at" and "created_at" columns separately every time for each new table?

like image 901
krn Avatar asked Feb 19 '11 22:02

krn


1 Answers

From memory, the mysql timestamp column type behaves similar to updated_at in that it is updated with the current time whenever the record is updated.

While this is useful for the updated_at column, this is not the desired behaviour for created_at.

In addition, Rails handles the timezone as specified in your app's settings (should would normally be set to UTC), so using mysql's time may be inconsistent with other datetime records.

like image 62
Zubin Avatar answered Nov 02 '22 06:11

Zubin