kalkatfyodor
Why isn't Phoenix using `utc_datetime` and `timestamptz`?
But instead using native_datetime and timestamp.
I have searched the Internet and the overwhelming majority is saying that timestamp should NOT be used and instead one should use timestamptz.
So why does Phoenix use native_datetime and timestamp?
Sources:
Most Liked
Nicd
Please use the search facilities of this forum, just a search for timestamptz returns several topics that have details about this.
The reason why Ecto’s timestamps() are naive by default is backwards compatibility. Back when Ecto was created, Elixir didn’t even have datetime types and Ecto didn’t have timezone support.
Also quoting my earlier answer in an earlier topic:
BTW, if you need to store the timezone too (in PostgreSQL), you have no option but to use a naive datetime field and store the offset or timezone information separately. This is because PostgreSQL does not have a data type for storing a timestamp with timezone.
There is the confusingly named timestamp with timezone but all it does is convert your input timestamp to UTC and convert it back to whatever your DB connection’s timezone is when reading. It does not even store the offset/timezone. So you need to do that yourself somehow.
Personally I don’t find much use in timestamptz, but that’s up to everyone to decide for themselves. It’s easy to override this in your migrations. In your schema you would use something like timestamps(type: :utc_datetime) and in migration timestamps(type: :timestamptz).
If you’re using just Elixir and your application is the only one connecting to the database, then it doesn’t make a difference if you use timestamp or timestamptz. If you use other applications to also connect to the same database, timestamptz will have different implications.
dodo
No idea, I always use timestamptz.
In migrations: timestamps(type: :timestamptz)
In schemas: timestamps(type: :utc_datetime_usec)
al2o3cr
One of the answers in that linked StackOverflow post points to an official Postgres FAQ that has a decent-sounding reason:
(in response to "Don’t use timestamp without TZ to store UTC"):
When should you?
If compatibility with non-timezone-supporting databases trumps all other considerations.
Ecto already converts values to UTC, and needs to support other databases that don’t have timestamp with time zone.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








