Rails database-specific data type

时光毁灭记忆、已成空白 提交于 2019-12-10 11:15:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!