funboy

funboy

Add date to postgres by ecto

Hi All,
How could I insert to postgres date formatted like that -> “2020-01-14 12:00:00 UTC”

#migration
defmodule Hello.Repo.Migrations.Travels do
  use Ecto.Migration

  def change do
    create table(:travels, primary_key: false) do
      add(:arrived, :timezonetz, null: false, primary_key: true)
end


#travel.ex
defmodule Hello.Travel do
  use Ecto.Schema

   @primary_key false
   schema "travels" do
      field(:arrived, :utc_datetime, primary_key: true)
   end
end
> travel = %Hello.Travel{arrived: ~N[2020-01-16 10:35:00]}   
> Hello.Repo.insert(travel)

 (Ecto.ChangeError) value `~N[2020-01-16 10:35:00]` for `Hello.Travel.arrived` 
in `insert` does not match type :utc_datetime

the same is with

travel = %Hello.Travel{arrived: ~N[2018-12-30T16:00:00.000Z]}

Marked As Solved

NobbZ

NobbZ

ectos :utc_datetime primitive type only maps from a DateTime, your example though uses a NaiveDateTime.

Either use a DateTime as input, or use :naive_datetime in the DB.

Which solution is correct for you depends on the semantics of the value.

Also Liked

NobbZ

NobbZ

You do not change migrations that already have run. You create a new migration that alters the database schema.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement