Ecto.Changeset.no_assoc_constraint
You're seeing just the function
no_assoc_constraint
, go back to Ecto.Changeset module for more information.
Specs
Checks the associated field does not exist.
This is similar to foreign_key_constraint/3
except that the
field is inferred from the association definition. This is useful
to guarantee that parent can only be deleted (or have its primary
key changed) if no child exists in the database. Therefore, it only
applies to has_*
associations.
As the name says, a constraint is required in the database for this function to work. Such constraint is often added as a reference to the child table:
create table(:comments) do
add :post_id, references(:posts)
end
Now, when deleting the post, it is possible to forbid any post to be deleted if they still have comments attached to it:
post
|> Ecto.Changeset.change
|> Ecto.Changeset.no_assoc_constraint(:comments)
|> Repo.delete
Options
:message
- the message in case the constraint check fails, defaults to "is still associated with this entry" (forhas_one
) and "are still associated with this entry" (forhas_many
):name
- the constraint name. By default, the constraint name is inferred from the association table + association field. May be required explicitly for complex cases