# in my migration
create table(:users_account) do
add :name, :string
timestamps()
end
create unique_index("users_account", [:tag])
# in schema
schema "users_account" do
field(:name, :string)
timestamps()
end
def changeset(clash_accounts, attrs) do
clash_accounts
|> cast(attrs, [:name, :tag, :townhall])
|> validate_required([:name, :tag, :townhall])
|> unique_constraint(:name,
name: :user_tag_index,
message: "USER_EXISTS_WITH_SAME_NAME"
)
end
however, I get:
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* user_tag_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 has not defined any constraint.
It may also be that you are inserting a User struct directly and not a changeset. I recall running into this before and thinking the index name was incorrect but I was not inserting a changeset.
@spizzy do you have a minimal public repo of the code?. It would be easier for anyone to jump in for helping you figure out the issue that you’re facing.
Also, posting the error message will provide more clarity on the issue.
You don’t have to set the name in the migration file, if you are planning to use default index name.
create unique_index(:users_account, [:tag]) should be enough in the migration file.
Based on your previous inputs, this should likely fix your issue… For further help or queries, it’s better to provide some kind of a minimal repo that can be used to replicate the issue.