ruby-on-rails - 如何更改 rails migration t.timestamps 以在 postgres 中使用 `timestamp(0) without timezone`

标签 ruby-on-rails postgresql datetime timestamp

我正在尝试弄清楚如何更改 t.timestamps 在 Rails 迁移中使用的 native 数据类型。在 postgres 中结束的默认类型是 timestamp without timezone。我想要的是 timestamp(0) without timezone

我想更改 native 数据类型,以便在创建新表并在迁移中使用 t.timestamps 时,它会自动创建正确的时间戳数据类型。

我需要 timestamp(0) without timezone 因为我的 rails 应用程序与 laravel 应用程序共享它的数据库,并且两个应用程序都可以插入数据。由于 rails 使用毫秒/laravel 不使用,并且似乎没有办法(截至 2018-10-23)让 laravel 支持拥有包含不同格式时间戳的表(Y-m-d H:i:s.u vs Y-m-d H:i:s) 而不必关闭模型中的时间戳,基本上禁用它们的自动管理,我想要数据库强制使用单一格式(Y-m-d H:i:s)。

有关更多详细信息,请参阅我的其他问题:Is there a way to change Rails default timestamps to Y-m-d H:i:s (instead of Y-m-d H:i:s.u) or have laravel ignore decimal portion of Y-m-d H:i:s.u?

所以我想使用 timestamp(0) 来截断毫秒,而不必考虑在创建新表时正确设置表时间戳类型,因为 native 类型已经是 时间戳(0)

我试过了

./config/environments/initializers

require "active_record/connection_adapters/postgresql_adapter"

module ActiveRecord
 module ConnectionAdapters
   class PostgreSQLAdapter
     NATIVE_DATABASE_TYPES.merge!(
      timestamp: { name: "timestamp(0) without timezone" }
     )
   end
 end
end

和类似的迁移

class ChangeTimestampTypesToTimestamp0 < ActiveRecord::Migration[5.2]
  def change
    create_table :test, id: :uuid, default: -> { "gen_random_uuid()" } do|t|
      t.string :name, null: false

      t.timestamps
    end
  end
end

但这没有用。

我还尝试将时间戳更改为使用 timestampz 进行上述相同的迁移作为健全性检查,仍然没有运气......

require "active_record/connection_adapters/postgresql_adapter"

module ActiveRecord
 module ConnectionAdapters
   class PostgreSQLAdapter
     NATIVE_DATABASE_TYPES.merge!(
       timestamp: { name: "timestamptz" }
     )
   end
 end
end

enter image description here

最佳答案

我相信我已经弄明白了!

我开始研究通过从控制台打印出变量来设置 NATIVE_DATABASE_TYPES

Rails c
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES

结果: {:primary_key=>"bigserial primary key", :string=>{:name=>"character varying"}, :text=>{:name=>"text"}, :integer=>{:name =>"整数", :limit=>4}, :float=>{:name=>"float"}, :decimal=>{:name=>"decimal"}, :datetime=>{:name=> "时间戳"}, :time=>{:name=>"time"}, :date=>{:name=>"date"}, :daterange=>{:name=>"daterange"}, :numrange= >{:name=>"numrange"}, :tsrange=>{:name=>"tsrange"}, :tstzrange=>{:name=>"tstzrange"}, :int4range=>{:name=>"int4range "}, :int8range=>{:name=>"int8range"}, :binary=>{:name=>"bytea"}, :boolean=>{:name=>"boolean"}, :xml=>{ :name=>"xml"}, :tsvector=>{:name=>"tsvector"}, :hstore=>{:name=>"hstore"}, :inet=>{:name=>"inet"} , :cidr=>{:name=>"cidr"}, :macaddr=>{:name=>"macaddr"}, :uuid=>{:name=>"uuid"}, :json=>{:name =>"json"}, :jsonb=>{:name=>"jsonb"}, :ltree=>{:name=>"ltree"}, :citext=>{:name=>"citext"}, : point=>{:name=>"point"}, :line=>{:name=>"line"}, :lseg=>{:name=>"lseg"}, :box=>{:name=> "box"}, :path=>{:name=>"path"}, :polygon=>{:name=>"polygon"}, :circle=>{:name=>"ci rcle"}, :bit=>{:name=>"bit"}, :bit_varying=>{:name=>"bit varying"}, :money=>{:name=>"money"}, :interval= >{:name=>"interval"}, :oid=>{:name=>"oid"}

事实证明,在我开始将它包含在我的

之前,timestamp 从未真正设置过
module ActiveRecord
 module ConnectionAdapters
   class PostgreSQLAdapter
     NATIVE_DATABASE_TYPES.merge!(
      timestamp: { name: "timestamp", limit:0 }
     )
   end
 end
end

包含的内容以为是 datetime,我意识到 timestampdatetime 的别名。

我将 NATIVE_DATABASE_TYPES 合并更改为如下所示...

require "active_record/connection_adapters/postgresql_adapter"

module ActiveRecord
 module ConnectionAdapters
   class PostgreSQLAdapter
     NATIVE_DATABASE_TYPES.merge!(
       datetime: { name: "timestamp", limit:0 }
     )
   end
 end
end

我运行了迁移,列已成功设置为 timestamp(0) without timezone

关于ruby-on-rails - 如何更改 rails migration t.timestamps 以在 postgres 中使用 `timestamp(0) without timezone`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52953885/

相关文章:

ruby-on-rails - 将对象作为路径助手的参数传递

sql - Rails 搜索 SQL 查询

ruby-on-rails - 将参数括起来以确保该 block 将与方法调用相关联

sql - 使用 postgres 中存储过程的输出创建一个临时表

ruby-on-rails - 使用 Google 的 Audit API 监控 google 应用程序电子邮件

ruby-on-rails - Postgres 无法连接到服务器

arrays - 将 HSTORE 数组值转换为 JSONB 中的列

java - 在当前日期上添加天数

javascript - 这个时间计算脚本中的奇怪错误?

mysql - 在数据库中存储特定的日期时间格式