Validate unique_index inside changeset

I’m trying to validate a custom unique_index where the parent_id is NULL inside a changeset without success.

Changeset:

changeset
|> unique_constraint(:name, name: :pizza)

migration:

def change do
  create table(:reaction) do
    add :name, :string
    add :parent_id, references(:reaction)
  end

create unique_index(:reaction, :name, where: "parent_id IS NULL", name: :pizza)
end

:wave:

It’s not a validation, it’s a “constraint”. It’d package the error in a changeset if you trip up the unique index constraint, but it wouldn’t validate the reaction being actually unique in the changeset/2, you’d need to use pipe the changeset through one of the Repo functions (like Repo.insert/1) for it.