How to Set a Unique Constraint to a Table Field
From ElixirBlocks
This article requires that you know: How to Generate a Migration File
If you already have a table and retroactively want to set a field to only allow unique values, you need to create a migration with:
create unique_index(:table_name, [:field_name])
This is a full example where a table named :groups has a field name :name set to a unique constraint.
defmodule App.Repo.Migrations.EnsureGroupNameIsUnique do use Ecto.Migration def change do create unique_index(:groups, [:name]) end end