addict icon indicating copy to clipboard operation
addict copied to clipboard

Email uniqueness not being enforced properly

Open jaimeiniesta opened this issue 8 years ago • 4 comments

Hello, according to the documentation,

Addict by default validates that the password is at least 6 characters long and the e-mail is valid and unique.

But, out of the box, it does not validate the uniqueness of the email. It lets me insert 2 users with the same email.

Now, if I add a unique index on the database table with create unique_index(:users, [:email]), I prevent that from happening at the database layer, but the DB error is not converted to a changeset error as unique_constraint is supposed to do, a 500 internal server error is raised.

jaimeiniesta avatar May 25 '16 14:05 jaimeiniesta

the error happens when you try to save the record at InsertUser.call(schema, user_params, repo) here, when you create the structure struct(schema, user_params),It does not have constraints defined:

user_params = for {key, val} <- user_params, into: %{}, do: {String.to_atom(key), val}
  schema
  |> cast(user_params, ~w(....))
  |> unique_constraint(:email)
  |> repo.insert()

The constraints can only be checked in a safe way when performing the operation in the database. As a consequence, validations are always checked before constraints. Constraints won’t even be checked in case validations failed.

jcortesg avatar Jun 03 '16 16:06 jcortesg

If I add a unique_constraint(:email) on my User model I get this error. Is there an existing way to add a unique constraint to a User model while using Addict?

morgz avatar Sep 02 '16 09:09 morgz

I just started tinkering with Addict and i'm running into this issue as well.
It just ignores the unique_constraint in my User model and throws an error:

* (Ecto.ConstraintError) constraint error when attempting to insert struct:
    * unique: users_email_index

jacklin10 avatar Oct 05 '16 20:10 jacklin10

Still happening to me with a fresh phoenix and addict project. Anyone else still having this issue as well?

acroca avatar Sep 06 '17 08:09 acroca