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 without timezone
. What I would like instead is timestamp(0) without timezone
.
I'd like to change the native data type so that when a new table is created and t.timestamps
is used in the migration, it automatically creates the correct timestamp data type.
I require timestamp(0) without timezone
because my rails application shares its DB with a laravel application and both applications can insert data. Due to the fact that rails uses milliseconds/laravel does not, and there doesn't seem to be a way ( as of 2018-10-23 ) for laravel to support having a table that contains different formats of timestamps (Y-m-d H:i:s.u
vs Y-m-d H:i:s
) without having to turn off timestamps in the model, essentially disabling the auto management of them, I'd like to have the database enforce the use of a single format(Y-m-d H:i:s
).
For more details please my other question: 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?
So I want to use timestamp(0)
to truncate the milliseconds and not have to think about setting the tables timestamp types correctly when creating a new table, as the native type would already be timestamp(0)
I've tried this
./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
and a migration like
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
but that did not work.
I also tried to change the timestamp to use timestampz with the same migration above as a sanity check, still no luck...
require "active_record/connection_adapters/postgresql_adapter"
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter
NATIVE_DATABASE_TYPES.merge!(
timestamp: { name: "timestamptz" }
)
end
end
end