I have been having this error why trying to run MIX TEST…Please can someone help me with this
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* item_categories_name_index (unique_constraint)
If you would like to stop this constraint violation from raising an
exception and instead add it as an error to your changeset, please
call `unique_constraint/3` on your changeset with the constraint
`:name` as an option.
The changeset defined the following constraints:
* item_categories_pickup_centre_id_fkey (foreign_key_constraint)
* item_categories_name_pickup_centre_id_index (unique_constraint)
This error is saying that Ecto gets a constraint violation error from
item_categories_name_index when trying to perform an insert.
Ecto.Changeset provides functions like
unique_constraint to tell the adapter to transform those errors into validation errors, but the “expected” errors need to be listed explicitly. The final part of this error message states that the changeset did declare some of them, just not the one that actually raised an error.
Your test(s) are trying to insert record(s) that break a table-level uniqueness constraint – namely the DB is telling you “sorry I can’t insert that record bro, the constraint you put in says that it’s a duplicate”.
How to deal with it – well, @al2o3cr already gave you a good starting point.