Reputation: 3211
I have have a User model with an email field. Now I would like to make it unique, so, as per documentation, I need to apply:
cast(user, params, ~w(email), ~w())
|> unique_constraint(:email)
Also, I should define the unique index in a migration:
create unique_index(:users, [:email])
The problem is that when I tried to define this in a migration while adding some more fields it didn't work and now I'm trying to just define a migration with this create unique_index(:users, [:email])
and it's creating an error:
[info] create index users_email_index
** (Postgrex.Error) ERROR (unique_violation): could not create unique index "users_email_index"
What am I doing wrong?
Upvotes: 11
Views: 3462
Reputation: 84140
This can happen when the unique constraint is already violated in your table.
Please check that you do not already have duplicate email addresses in your users table.
You can run mix do ecto.drop, ecto.create, ecto.migrate
to delete and recreate the database and tables.
Upvotes: 15