我有一个在 postgres 数据库上运行的 rails 应用程序。我有一个名为 UserTimings 的模型,其中有两个字段 from_time 和 to_time。
t.time "from_time"
t.time "to_time"
我预计时间将以带有时区信息的完整 UTC 格式存储。过了一会儿,我意识到数据库有这种创建表的 SQL 查询。
from_time time without time zone,
to_time time without time zone,
我不想要这个。我想存储时区。我想要 UTC 时间的 +0530 东西,虽然 Rails 应用程序已配置为处理它,但我没有得到。请告诉我如何编写迁移以强制数据库使用时区。
谢谢。
最佳答案
迁移完全按照您的意愿进行
class CreatePeppers < ActiveRecord::Migration
def change
create_table :peppers do |t|
t.column :runs, 'timestamp with time zone'
t.column :stops, 'time with time zone'
end
end
end
类型供您选择。你可以在这里写任何 postgresql 支持的类型。但是转换为类型可能会出现问题,rails 可以理解。
关于ruby-on-rails - rails :How to create a time column with timezone on postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19207856/