Customer can create a contact with name and phone number.
2, Customer can not create a contact with already existing phone number.
So I did like this…
schema "people" do
field :name, :string
field :phone_number, :string
belongs_to :phonebook, Phonebook
timestamps()
end
def changeset(%Person{} = person, attrs) do
person
|> cast(attrs, [:name, :phone_number])
|> validate_required([:name, :phone_number])
|> unique_constraint(:phone_number)
end
And then set the constraint in the changeset accordingly. It is the exact same situation as described in the complex constraint section you linked. It’s just that your names are a bit different.