Error raised, even with on_conflict: :nothing

I tried to insert records as:

Repo.insert_all(MyApp.Node, db_records, on_conflict: :nothing)

But I got this error:

[error] Task #PID<0.1547.0> started from #PID<0.1546.0> terminating
** (Postgrex.Error) ERROR (unique_violation): duplicate key value violates unique constraint "nodes_unique_name_uindex"

table: nodes
constraint: nodes_unique_name_uindex

Key (unique_name)=(Helen) already exists.
    (ecto) lib/ecto/adapters/sql.ex:187: Ecto.Adapters.SQL.query!/5
    (ecto) lib/ecto/adapters/sql.ex:375: Ecto.Adapters.SQL.insert_all/8
    (ecto) lib/ecto/repo/schema.ex:48: Ecto.Repo.Schema.do_insert_all/6
    (trackware) web/controllers/csvs_controller.ex:81: Trackware.CsvsController.write_csv_records/1
    (elixir) lib/task/supervised.ex:85: Task.Supervised.do_apply/2
    (elixir) lib/task/supervised.ex:36: Task.Supervised.reply/5
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

So, shouldn’t the on_conflict: :nothing suppress such error?

1 Like

Hmm maybe it didn’t figure out the column that has unique constraint… Have you tried specifying: conflict_target: :name to point Ecto to right unique column?

1 Like

Did you forget to update Ecto to latest version?

The on_conflict was not present in 2.0.6, you need at least 2.1:
https://hexdocs.pm/ecto/2.0.6/Ecto.Repo.html#c:insert/2

2 Likes

in my deps:

{:phoenix, "~> 1.2.0"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.0-rc"},

how can I update ecto? is it phoenix_ecto it self?

1 Like

Update phoenix_ecto to 3.2.1

2 Likes

Thanks, I did and its working now

2 Likes