Ensuring an embedded schema field is unique when used in embeds_many

I have the ff. Parent schema:

defmodule Parent do
  use Ecto.Schema
                               
  schema "parents" do                                                            
    field :name, :string                                                         
    embeds_many :children, Child                                                 
                                                                             
    timestamps()                                                                 
  end
end

How do I ensure that one of the fields of Child is unique? I tried setting the field as the primary key like so:

defmodule Child do                                                               
  use Ecto.Schema
                                                                             
  @primary_key {:name, :string, []}                                              
                                                                             
  embedded_schema do                                                             
    field :age, :integer                                                         
  end
end

But I was still able to insert children with the same names.

1 Like