I have a patient-complaints structure.
A patient has many complaints.
The patient has an unique index name.
When I supply the patient-name and a list of complaints, I would like the upsert to exchange the old complaints with the new complaints (deleting the old ones)
I did:
def save(params) do
cs = Patient.changeset(%Patient{}, params)
Repo.insert(cs, on_conflict: :replace_all_except_primary_key, conflict_target: :name)
end
def changeset(patient, attrs) do
patient
|> cast(attrs, [:name])
|> validate_required([:name])
|> validate_length(:name, min: 1)
|> unique_constraint(:name)
|> cast_assoc(:complaints, with: &BasicPatientInfo.Complaint.changeset/2)
end
def changeset(complaint, attrs) do
complaint
|> cast(attrs, [:complaint_uuid])
|> validate_required([:complaint_uuid])
|> validate_length(:complaint_uuid, is: 36)
end
Unfortunately, this keeps the old complaints and adds the new ones.
Is there a way to let ecto replace the complaints?