diff --git a/src/main/java/edu/harvard/iq/dataverse/DataversePage.java b/src/main/java/edu/harvard/iq/dataverse/DataversePage.java index 18886ce41a2..9dc1b927ce3 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DataversePage.java +++ b/src/main/java/edu/harvard/iq/dataverse/DataversePage.java @@ -611,6 +611,7 @@ public String save() { // so we skip looking at parents (which get set automatically with their children) if (!dsft.isHasChildren() && dsft.isRequiredDV()) { boolean addRequiredInputLevels = false; + boolean parentAlreadyAdded = false; if (!dsft.isHasParent() && dsft.isInclude()) { addRequiredInputLevels = !dsft.isRequired(); @@ -622,9 +623,20 @@ public String save() { if (addRequiredInputLevels) { listDFTIL.add(new DataverseFieldTypeInputLevel(dsft, dataverse,true, true)); - //also add the parent as required + //also add the parent as required (if it hasn't been added already) if (dsft.isHasParent()) { - listDFTIL.add(new DataverseFieldTypeInputLevel(dsft.getParentDatasetFieldType(), dataverse,true, true)); + DataverseFieldTypeInputLevel parentToAdd = new DataverseFieldTypeInputLevel(dsft.getParentDatasetFieldType(), dataverse, true, true); + for (DataverseFieldTypeInputLevel dataverseFieldTypeInputLevel : listDFTIL) { + if (dataverseFieldTypeInputLevel.getDatasetFieldType().getId() == parentToAdd.getDatasetFieldType().getId()) { + parentAlreadyAdded = true; + break; + } + } + if (!parentAlreadyAdded) { + // Only add the parent once. There's a UNIQUE (dataverse_id, datasetfieldtype_id) + // constraint on the dataversefieldtypeinputlevel table we need to avoid. + listDFTIL.add(parentToAdd); + } } } }