Creating Unique Constraint/Index Ecto.Migration: Elixir

Have you ever found yourself in a position where you can find anything about something you really need? Just happen to encounter this problem doing my task for a company I’m working with and I can’t find proper documentation about it. So here it is, hope I'm the last one who had troubles trying and finding the right way to do this.


Let's start with the migration, say you want to create a movie table with two fields (title and genre) and you don't want to create entries with duplicate values of those fields.

~w(title genre)a,
name: :index_for_movies_duplicate_entries

index_for_movies_duplicate_entries: can be any name you want, can also be automatically generated based on the fields included on your constraint.

~w(title genre)a: a list of fields to checks for a unique constraint. Can also be [:title, :genre], I just like the sigil one.


Make sure to pipe the unique_constraint/3 on your changeset.

 schema "movies" do
field(:genre, :string)
field(:title, :string)
@required ~w(title)a
@optional ~w(genre)a
def changeset(struct, params \\ %{}) do
|> cast(params, @optional ++ @required)
|> validate_required(@required)
|> unique_constraint(
name: :index_for_movies_duplicate_entries


The 2nd parameter of unique_constraint/3 can be any field that’s a part of the constraint, in this case, the title.


Wanna know how to test this? check out the full story on my website: Creating Unique Constraint/Index Ecto.Migration: Elixir

And there you have it. hope this can help. 🎉

I drink(☕️) and I code things —

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store