问题
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.
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.
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?
I understand ActiveRecord tries to be database-agnostic, but this is just a pet peeve I cannot ignore.
回答1:
I would suggest taking a look at this answer (very good explanation): Ignoring timezones altogether in Rails and PostgreSQL
UPDATE
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 Documentation:
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/static/datatype-datetime.html
回答2:
We can use this gem to override the configuration and use Adapter specific datatypes.
来源:https://stackoverflow.com/questions/11713791/rails-database-specific-data-type