There is no unique or exclusion constraint matching the ON CONFLICT specification

I am trying to Repo.insert_all into table where driver_name is unique with respect to school_id

The constraint for driver_name is drivers_driver_name_is_assistants_school_id_unique
however, I get this error:

** (Postgrex.Error) ERROR 42P10 (invalid_column_reference): there is no unique or exclusion constraint matching the ON CONFLICT specification
    (ecto) lib/ecto/adapters/sql.ex:195: Ecto.Adapters.SQL.query!/5
    (ecto) lib/ecto/adapters/postgres.ex:86: Ecto.Adapters.Postgres.insert_all/7
    (ecto) lib/ecto/repo/schema.ex:55: Ecto.Repo.Schema.do_insert_all/7
    (trackware) web/controllers/csvs_controller.ex:454: Trackware.CsvsController.write_ecto_rows_schools/4

At line:

{inserted_count, inserted_records}= Trackware.Schools.Repo.insert_all(Trackware.Schools.Driver, ecto_rows, on_conflict: :nothing, returning: [:driver_name], conflict_target: :driver_name)

Any idea?

1 Like

Hi, I had a similar problem and solved it by adding both fields included in the constraint as values for conflict_target.

May by using conflict_target: [:school_id, :driver_name] will solve the problem.