Ecto.Changeset.validate_unique/3

Parsed documentation:
View on GitHub
Validates `field`'s uniqueness on `Repo`.

## Examples

    validate_unique(changeset, :email, on: Repo)


## Options

  * `:on` - the repository to perform the query on
  * `:downcase` - when true, downcase values when performing the uniqueness query

## Case sensitivity

Unfortunately, different databases provide different guarantees
when it comes to case sensitive. For example, in MySQL, comparisons
are case insensitive. In Postgres, users can define case insensitive
column by using the `:citext` type/extension.

Those facts make it hard for Ecto to guarantee if the unique
validation is case insensitive or not and therefore it **does not**
provide a `:case_sensitive` option.

However this function does provide a `:downcase` option that
guarantees values are downcased when doing the uniqueness check.
When you set this option, values are downcased regardless of the
database you are using.

Since the `:downcase` option downcases the database values on the
fly, use it with care as it may affect performance. For example,
if you must use this option, you may want to set an index with the
downcased value. Using `Ecto.Migration` syntax, one could write:

    create index(:posts, ["lower(title)"])

Many times though, you don't even need to use the downcase option
at `validate_unique/3` and instead you can explicitly downcase
values before inserting them into the database:

    cast(params, model, ~w(email), ~w())
    |> update_change(:email, &String.downcase/1)
    |> validate_unique(:email, on: Repo)
Suggestions:
Please help! Open an issue on GitHub if this assessment is incorrect.