Issues creating a unique_constraint - Throwing exception

I recently posted a question in relation to this error on another model. The solutions provided worked like a charm, but I am having problems setting this up on the next model.

The error i am getting goes:

    * events_bitid_index (unique_constraint)

If you would like to stop this constraint violation from raising an
exception and instead add it as an error to your changeset, please
call `unique_constraint/3` on your changeset with the constraint
`:name` as an option.

I have the following index created:

   create unique_index(:events, [:bitid])

and my changeset looks like:

  def changeset(event, attrs) do
    event
    |> cast(attrs, [:artist_id, :datetime, :description, :bitid, :lineup, :url])
    |> validate_required([:artist_id, :datetime, :description, :bitid, :lineup, :url])
    |> unique_constraint(:bitid, name: :events_bitid_index)
  end

when I insert a record with and existing :bitid , I am getting the error above.

Thanks!

how does your insert code look like? ie are you using the changeset?

1 Like

ah! i was just inserting a struct, NOT a changeset… GOOD CALL!

fixed…

Thanks!

2 Likes