unique_constraint, I don't know how to use :name

Can somebody please explain to me how to use :name parameter? I have unique index field for email & username. When i use simple unique_constraint() on both in changeset i get following error. I understand it but when i tried to use :name… no succeess. Thanks.

constraint error when attempting to insert struct:

    * unique: veil_users_email_username_index

If you would like to convert this constraint into an error, please
call unique_constraint/3 in your changeset and define the proper
constraint name. The changeset defined the following constraints:

    * unique: veil_users_username_index
    * unique: veil_users_email_index

The error actually tells you all you need. The index in the db is veil_users_email_username_index, while the constaints in your changeset work for veil_users_username_index or veil_users_email_index. So you’d need to add a constraint for the first name and you can probably skip the two others.

1 Like
struct
|> cast(...)
|> validate_required(...)
|> unique_constraint(:email, name: :veil_users_email_username_index, message: "Hello there. I failed.")

:message is optional

1 Like