Let’s say I have the below migration, schema and function. I want something_happened_at
to be stored in postgres as UTC datetime object with the timezone offset. Right now I can only get the UTC time without offset into postgres datetime object. The datetime comes in as an iso 8601 string. DateTime.from_iso8601
sheds the offset when i convert it so I seemingly have no way to send to postgres with the offset other than storing the iso 8601 as a string and parsing it when I retrieve it. Any ideas on an alternate method to accomplish this?
MIgration
defmodule MyApp.Repo.Migrations.SomeMigration do
use Ecto.Migration
def change do
create table(:some_table) do
add(:user, references("users", on_delete: :nothing))
add(:something_happened_at, :utc_datetime)
timestamps()
end
end
end
Schema
defmodule MyApp.Something do
use Ecto.Schema
import Ecto.Changeset
alias MyApp.{User}
schema "some_table" do
field(:something_happened_at, :utc_datetime_usec)
belongs_to(:user, User, on_replace: :nilify)
timestamps()
end
def changeset(something, attrs \\ %{}) do
something
|> cast(attrs, [
:user_id,
:something_happened_at
])
|> validate_required([:something_happened_at])
end
end
Function to create or update entry
def do_date_thing(date) do
attrs = %{
something_happened_at: DateTime.from_iso8601(date)
}
# ...send attrs to changeset
# ...update or insert entry
end