Ecto.Changeset.unsafe_validate_unique
unsafe_validate_unique
, go back to Ecto.Changeset module for more information.
Specs
Validates that no existing record with a different primary key has the same values for these fields.
This function exists to provide quick feedback to users of your
application. It should not be relied on for any data guarantee as it
has race conditions and is inherently unsafe. For example, if this
check happens twice in the same time interval (because the user
submitted a form twice), both checks may pass and you may end-up with
duplicate entries in the database. Therefore, a unique_constraint/3
should also be used to ensure your data won't get corrupted.
However, because constraints are only checked if all validations succeed, this function can be used as an early check to provide early feedback to users, since most conflicting data will have been inserted prior to the current validation phase.
Options
:message
- the message in case the constraint check fails, defaults to "has already been taken".:match
- how the changeset constraint name is matched against the repo constraint, may be:exact
or:suffix
. Defaults to:exact
.:suffix
matches any repo constraint whichends_with?
:name
to this changeset constraint.:error_key
- the key to which changeset error will be added when check fails, defaults to the first field name of the given list of fields.:prefix
- the prefix to run the query on (such as the schema path in Postgres or the database in MySQL). SeeEcto.Repo
documentation for more information.:repo_opts
- the options to pass to theEcto.Repo
call.
Examples
unsafe_validate_unique(changeset, :city_name, repo)
unsafe_validate_unique(changeset, [:city_name, :state_name], repo)
unsafe_validate_unique(changeset, [:city_name, :state_name], repo, message: "city must be unique within state")
unsafe_validate_unique(changeset, [:city_name, :state_name], repo, prefix: "public")