I have a table in which I have put unique constraints on the combination of two fields. I had also defined this in changeset.
def changeset(asset, attrs) do
asset
|> cast(attrs, @create_attrs)
|> validate_required(@required_attrs)
|> unique_constraint([:serial_number, :organization_id])
end
But I keep getting this error instead of the object {:error, #Ecto.Changeset…} -
Server: localhost:4000 (http)
Request: POST /api/create_asset
** (exit) an exception was raised:
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* "assets_organization_id_serial_number_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.
Until I found out that order also matters when writing the unique_constraint. It worked when i changed it to -
unique_constraint([:organization_id, :serial_number])
So I just wanted to put it here, so that we can fix this bug in the new release. Would also love to contribute if I’m able to fix it by myself.
Thanks