addict
addict copied to clipboard
Email uniqueness not being enforced properly
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.
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.
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?
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
Still happening to me with a fresh phoenix and addict project. Anyone else still having this issue as well?