Ruby on Rails的:为什么ActiveRecord的使用"日期时间"类型而不是"时间戳"时间戳字段? [英] 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
两列,如我所料,创建的updated_at和created_at。然而,他们的类型是日期时间,而不是时间戳。
Two columns, as I expected, were created: "updated_at" and "created_at". However, their type is "datetime" and not "timestamp".
我使用MySQL和时间戳型,据我所知,的设计正是为了这样的情况下一>,因为它使用较少的空间,是独立的时区。
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.
那么,有没有任何理由,为什么Rails 3中使用日期时间,而不是时间戳?我应该尝试解决这个问题?如果是的话,有没有办法,除了不使用t.timestamps每一次分别定义的updated_at和created_at栏目为每个新表来做到这一点?
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?
推荐答案
从内存中,MySQL的时间戳
列类型的行为类似于的updated_at
的,因为它与每当记录被更新为当前时间更新。
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.
虽然这是针对的updated_at
列有用的,这不是 created_at
所需的行为。
While this is useful for the updated_at
column, this is not the desired behaviour for created_at
.
此外,Rails的处理在你的应用程序的设置规定(应该通常被设定为UTC),因此使用MySQL的时间可能与其他日期时间记录不一致时区。
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.
这篇关于Ruby on Rails的:为什么ActiveRecord的使用"日期时间"类型而不是"时间戳"时间戳字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!