how to change rails migration t.timestamps to use `timestamp(0) without timezone` in postgres

后端 未结 2 881
一整个雨季
一整个雨季 2021-01-25 13:54

I\'m trying to figure out how to change the native data type that t.timestamps uses in a rails migration. Default type that ends up in postgres is timestamp w

2条回答
  •  萌比男神i
    2021-01-25 14:31

    I believe I've figure it out!

    I started looking into what NATIVE_DATABASE_TYPES were bring set by print out the variable from the console

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

    Result: {:primary_key=>"bigserial primary key", :string=>{:name=>"character varying"}, :text=>{:name=>"text"}, :integer=>{:name=>"integer", :limit=>4}, :float=>{:name=>"float"}, :decimal=>{:name=>"decimal"}, :datetime=>{:name=>"timestamp"}, :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=>"circle"}, :bit=>{:name=>"bit"}, :bit_varying=>{:name=>"bit varying"}, :money=>{:name=>"money"}, :interval=>{:name=>"interval"}, :oid=>{:name=>"oid"}

    turns out that timestamp was never actually set before I started including it with my

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

    What was being included thought was datetime and I realized that timestamp was an alias of datetime.

    I changed the NATIVE_DATABASE_TYPES merge to look like this...

    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
    

    I ran my migration and the columns were successfully set to timestamp(0) without timezone

提交回复
热议问题