Rails:如何在Postgres上创建带有时区的时间列 [英] Rails:How to create a time column with timezone on postgres

查看:96
本文介绍了Rails:如何在Postgres上创建带有时区的时间列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rails 应用程序,该应用程序可用于 postgres 数据库。我有一个名为UserTimings的模型,其中有两个字段from_time和to_time。

I have a rails application which works on postgres db. I have a model called UserTimings where there are two fields from_time and to_time.

t.time     "from_time"
t.time     "to_time"

我希望时间将以带有时区信息的完整UTC格式存储。过了一会儿,我意识到数据库具有用于创建表的这种SQL查询。

I expected that the time will be stored in complete UTC format with timezone information. After a little while, I realized that the database has this kind SQL query to create the table.

from_time time without time zone,
to_time time without time zone,

我不希望这样。我想使用时区进行存储。我希望在UTC时间内无法解决+0530的问题,尽管已将rails应用程序配置为可以处理它。请告诉我如何编写迁移以强制数据库使用时区。

I do not want this. I want to store with time zone. I want the +0530 thingy in the UTC time which I'm not getting althought the rails application has been configured to handle it. Please tell me how to write a migration to force the db to use the time zone.

谢谢。

推荐答案

该迁移完全可以完成您想要的

That migration do exactly what you want

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可以理解,转换为类型可能会有问题。

Types are for your choice. You can write here any type, that postgresql supports. But there may be problems with conversion to types, that rails can understand.

这篇关于Rails:如何在Postgres上创建带有时区的时间列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆