Unique_constraint seems to allow duplicate entries

I am trying to keep dupes out of my database, but I think I am doing it wrong…

I have something like this:

  def changeset(artist, attrs) do
    |> cast(attrs, [:facebook_page_url, :bit_id, :image_url, :mbid, :name, :thumb_url, :url])
    |> validate_required([:name])
    |> unique_constraint(:name)
    |> unique_constraint(:bit_id)

Where I think I should be keeping duplicate records out, but I am finding several duplicate records in my database.

Ideas? Thanks!

Hi there!

Have you set up the unique_constraint on the database side to go with this? All unique_constraint is basically turn the database constraint error into a nicer error on the elixir side :slight_smile:


From the docs:

The unique constraint works by relying on the database to check if the unique constraint has been violated or not and, if so, Ecto converts it into a changeset error.



unique_constraint does only work if you have an actual unique constraint on the database level.

okay… my bad… i read that a million times, and read right through that…