迁移文件中的:datetime
和:timestamp
在MySQL和Sqlite3中似乎相同,它们都映射到数据库端的datetime
,除非在正式文档中找不到。
另外,如果我们的Rails项目何时可以使用其他DBMS,那么当我们对模型或脚手架进行:datetime
(或:timestamp
)使用时,应该使用script/generate
还是rails generate
呢?
最佳答案
我建议使用:datetime
,因为这样很清楚使用什么。我相信Rails在数据库中都使用DATETIME
是因为存在问题,即unix时间戳或MySQL TIMESTAMP
字段可以表示日期时间。由于默认情况下时间戳是32位整数(请参见Wikipedia:Timestamp),因此它只能表示1901-12-13
(如果不允许像MySQL这样的负值,则为1970-01-01
)和2038-01-19
之间的日期。之后或之前,它将溢出。这是year 2038 problem。
因此,为了让所有人都清楚,我在迁移中将其命名为:datetime
。
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
关于ruby-on-rails - 对于MySQL和Sqlite3,迁移文件中Rails的日期时间和时间戳是否相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3656392/