Foreign key constraint violation while upgrading to OpenMRS 2.0

I am facing foreign key constraint issue

Referential integrity constraint violation: “FK_5K7L7WK9OGYT1AG6VKU4A4LWO: PUBLIC.PATIENT FOREIGN KEY(PATIENT_ID) REFERENCES PUBLIC.PERSON(PERSON_ID) (3)”; SQL statement: insert into PATIENT (PATIENT_ID, CREATOR, DATE_CREATED, VOIDED) values (?, ?, ?, ?) [23506-187]

For following data

    <patient patient_id="2" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>

    <person person_id="3" gender="F" dead="false" creator="1" date_created="2008-08-15 15:46:47.0" voided="false" uuid="2b597be0-83c7-4f1d-b3d2-1d61ab128762"/>
    <person person_id="4" gender="F" dead="false" creator="1" date_created="2008-08-15 15:46:47.0" voided="false" uuid="2b597be0-83c7-4f1d-b3d2-1d61ab128823"/>
    <person person_id="5" gender="F" dead="false" creator="1" date_created="2008-08-15 15:46:47.0" voided="false" uuid="2b597be0-83c7-4f1d-b3d2-1d61ab128824"/>
    <person_name person_name_id="1" preferred="true" person_id="3" given_name="John" middle_name=" " family_name="Doe" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="0f1f7d08-076b-4fc6-acac-4bb91515141e"/>
    <person_name person_name_id="2" preferred="true" person_id="4" given_name="Jane" middle_name=" " family_name="Doe" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="0f1f7d08-076b-4fc6-acac-4bb91515141f"/>

    <patient patient_id="3" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>
    <patient patient_id="4" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>
    <patient patient_id="5" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>

This was working fine before upgrading to 2.0 since db constrains were off before 2.0. But now that db constriants are on, even thought person of id 3 is inserted before patient of id 3, it throws error.

When I move patient with id 2 down with the group of other patients, it works.

        <person person_id="3" gender="F" dead="false" creator="1" date_created="2008-08-15 15:46:47.0" voided="false" uuid="2b597be0-83c7-4f1d-b3d2-1d61ab128762"/>
        <person person_id="4" gender="F" dead="false" creator="1" date_created="2008-08-15 15:46:47.0" voided="false" uuid="2b597be0-83c7-4f1d-b3d2-1d61ab128823"/>
        <person person_id="5" gender="F" dead="false" creator="1" date_created="2008-08-15 15:46:47.0" voided="false" uuid="2b597be0-83c7-4f1d-b3d2-1d61ab128824"/>
        <person_name person_name_id="1" preferred="true" person_id="3" given_name="John" middle_name=" " family_name="Doe" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="0f1f7d08-076b-4fc6-acac-4bb91515141e"/>
        <person_name person_name_id="2" preferred="true" person_id="4" given_name="Jane" middle_name=" " family_name="Doe" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="0f1f7d08-076b-4fc6-acac-4bb91515141f"/>

        <patient patient_id="2" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>      
        <patient patient_id="3" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>
        <patient patient_id="4" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>
        <patient patient_id="5" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>

I am unable to understand what is causing this issue. Is it trying to insert all patients in the data set when it encounters the first patient?

@gauravd, just to check, does this error happen when you’re trying to load test data in a junit test?

yes, it happens on executeDataSet() call

@gauravd this looks strange. Can you share the unit test?

https://github.com/openmrs/openmrs-module-bedmanagement/blob/master/api/src/test/java/org/openmrs/module/bedmanagement/BedManagementServiceIntegrationTest.java

all of these tests will fail.

P.S: Mentioning the link as string because when i tried to add link, it said 'New members can post only 2 links in a post". Looks Weird!

@gauravd i have just cloned the module and compiled. All seems well. Or do you have a version of the module with your changes to reproduce what you are reporting?

here is a version with failing test “https://github.com/Gaurav-Deshkar/openmrs-module-bedmanagement/blob/master/api/src/test/java/org/openmrs/module/bedmanagement/BedManagementServiceIntegrationTest.java

@gauravd just completely remove the row for patient id 2 because it is a duplicate of a patient who is already inserted as part of standardTestDataset.xml in the test base class. Then all should be well.