Ignoring errors of insert_or_update

I want to retrieve the record that causes the error when I get error at using Ecto.insert_or_update
I get this error:

mobile_number: {"has already been taken", []}


** (MatchError) no match of right hand side value: {:error, #Ecto.Changeset<action: :insert, changes: %{created_at: #Ecto.DateTime<2017-04-27 17:35:20>, mobile_number: "6548", parent_type: "father"}, errors: [mobile_number: {"has already been taken", []}], data: #Trackware.Schools.Parent<>, valid?: false>}
    (trackware) web/controllers/csvs_controller.ex:203: anonymous fn/2 in Trackware.CsvsController.write_ecto_rows_schools/3
    (elixir) lib/enum.ex:1785: Enum.reduce_range_inc/4
    (trackware) web/controllers/csvs_controller.ex:190: Trackware.CsvsController.write_ecto_rows_schools/3
    (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

Can I suppress this error and retrieve the record that caused the error?

Sure, instead of matching to {:ok, blah} match it into a case statement instead. :slight_smile:

What about the record that caused the error? Can I get it?

PostgreSQL does not tell you, you have to query that yourself.

In that case, I have to check it such record exists before I try to insert a record

Or after, it all depends on your use-case, just be sure to do it in a transaction if it could be changed somewhere else too. :slight_smile: