Hello all.
I have this simple struct:
schema "domains" do
field :name, :string
field :redirect, :string
field :name_checked, :boolean, default: false
field :redirect_checked, :boolean, default: false
timestamps()
end
and this changeset:
def changeset(domain, params \\ %{}) do
domain
|> cast(params, [:name, :redirect, :name_checked, :redirect_checked])
|> unique_constraint(:name) # this is not working :(
|> validate_required([:name, :redirect])
end
However when testing the unique constraint on the name, it doesn’t work, here is my test:
test "uniqueness of name" do
url = "http://domain.com"
cs = Domain.changeset(%Domain{}, %{name: url, redirect: "http://redirect.com"})
Repo.insert!(cs)
cs2 = Domain.changeset(%Domain{}, %{name: url, redirect: "http://redirect2.com"})
assert {:error, changeset} = Repo.insert(cs2)
end
This test should pass as the second insert should match {:error, changeset}
, however, the second insert pass without raising the unique_constraint
…
I’m using ecto 2 with the ecto-2 branch here: https://github.com/michalmuskala/mongodb_ecto/tree/ecto-2
Anything I’m missing ?
Regards