Rails的数据库特定的数据类型 [英] Rails database-specific data type
问题描述
这是我第一次尝试了Rails的PostgreSQL的。我因缺乏时间戳和时区困惑
用法code我经常看到有关列存储绝对时间。一个典型的例子是<一个href="https://github.com/plataformatec/devise/blob/master/lib/generators/active_record/devise_generator.rb#L60"相对=nofollow>制定,它存储列的日期时间列,转化为时间戳与在PostgreSQL的时区
。
This is the first time I've tried out Rails with PostgreSQL. I am confused by the lack of timestamp with timezone
usage in code I usually see regarding columns storing absolute time. A prime example is devise, it stores columns a datetime column which translates to a timestamp with out timezone
in PostgreSQL.
我认为这是因为一种不好的做法TIMESTAMP WITH出来时区
意味着任何时区。如果你从另一个应用程序访问数据库,你将有不知道哪个时区不看Rails的时间戳引用。它只是变成一个分散的一塌糊涂。
I consider it a bad practice because timestamp with out timezone
could mean any time zone. If you were accessing the database from another application, you would have no idea which time zone the timestamp references without looking at Rails. It just becomes a decentralized mess.
有没有办法对我来说,在一个ActiveRecord的迁移/架构PostgreSQL的创建一个时间戳和时区
?将创建不兼容问题与当前的Rails框架code?
Is there any way for me to create a timestamp with time zone
in a ActiveRecord migration/schema for PostgreSQL? Will create incompatibility issues with the current Rails framework code?
我理解的ActiveRecord试图成为数据库无关,但是这仅仅是一个忌讳我也不能忽视。
I understand ActiveRecord tries to be database-agnostic, but this is just a pet peeve I cannot ignore.
推荐答案
我建议采取看看这个答案(很好的解释):<一href="http://stackoverflow.com/questions/9571392/ignoring-timezones-altogether-in-rails-and-postgresql">Ignoring时区干脆在Rails和PostgreSQL的
I would suggest taking a look at this answer (very good explanation): Ignoring timezones altogether in Rails and PostgreSQL
更新
据ActiveRecord的文档,上面的链接,和我自己的测试 - 我得出的结论是默认的ActiveRecord 日期时间
和时间戳
的架构迁移列类型不能修改为强制PostgreSQL的使用时间戳和时区
。
According to ActiveRecord documentation, the above link, and my own tests - I have concluded that the default ActiveRecord datetime
and timestamp
column types in schema migrations cannot be modified to force PostgreSQL to use timestamp with time zone
.
PostgreSQL文档:
注:SQL标准要求,写作只是时间戳是 相当于没有时间戳时区,和PostgreSQL荣誉的 行为。 (之前的发行7.3把它作为时间戳和时间 区。)timestamptz被接受作为与时间戳的缩写 时区;这是PostgreSQL扩展。
Note: The SQL standard requires that writing just timestamp be equivalent to timestamp without time zone, and PostgreSQL honors that behavior. (Releases prior to 7.3 treated it as timestamp with time zone.) timestamptz is accepted as an abbreviation for timestamp with time zone; this is a PostgreSQL extension.
http://www.postgresql.org/docs/9.1/静态/数据类型,datetime.html
这篇关于Rails的数据库特定的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!