Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email uniqueness not being enforced properly #85

Open
jaimeiniesta opened this issue May 25, 2016 · 4 comments
Open

Email uniqueness not being enforced properly #85

jaimeiniesta opened this issue May 25, 2016 · 4 comments
Labels

Comments

@jaimeiniesta
Copy link

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 jaimeiniesta changed the title Email uniqueness not being enforced propery Email uniqueness not being enforced properly May 25, 2016
@jcortesg
Copy link

jcortesg commented Jun 3, 2016

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.

@morgz
Copy link

morgz commented Sep 2, 2016

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?

@jacklin10
Copy link

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

@acroca
Copy link

acroca commented Sep 6, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants