Ecto unique_constraint error

I have the following migration:

def up do
    create(
      unique_index(
        :posts,
        [:post_page_url, :comment_id, :post_type],
        name: "post_post_page_url_comment_id_post_type_index"
      )
    )
  end

  def down do
    drop_if_exists(
      unique_index(
        :posts,
        [:post_page_url, :comment_id, :post_type],
        name: "post_post_page_url_comment_id_post_type_index"
      )
    )
  end

I can see that the same unique index exists in DB.

I added the following unique constraint to changeset

post
|> unique_constraint([:post_page_url, :comment_id, :post_type],
      name: :post_post_page_url_comment_id_post_type_index
    )

But I still get the unique constraint error stating:

 ** (Ecto.ConstraintError) constraint error when attempting to insert struct:
     
         * post_post_page_url_comment_id_post_type_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.
     
     The changeset has not defined any constraint.

I reset test and dev db several times but still the same error pops. Any help resolving this?

Can you show the offending insertion code?

1 Like