Cascade Deletion Not working

412 views
Skip to first unread message

goods...@gmail.com

unread,
Feb 20, 2020, 5:11:02 PM2/20/20
to HAPI FHIR

Hello All,

I am working with HAPI FHIR 4.1, DSTU2 deployment and trying to enable cascade deletion. My back end is Postgres SQL

Here are the steps I am following
- Added a new parameter in hapi.properties file and rebuilt / redeployed my application
cascading_delete.enabled=true

- Now when I pass the parameter to delete my Patient resource by passing the parameter _cascade=delete (or _cascade=true) it deletes the Patient resource but NOT its child resources
{
  "resourceType""OperationOutcome",
  "text": {
    "status""generated",
    "div""<div xmlns=\"http://www.w3.org/1999/xhtml\"><h1>Operation Outcome</h1><table border=\"0\"><tr><td style=\"font-weight: bold;\">information</td><td>[]</td><td><pre>Successfully deleted 1 resource(s) in 20ms</pre></td>\n\t\t\t\t\t\n\t\t\t\t\n\t\t\t</tr>\n\t\t</table>\n\t</div>"
  },
  "issue": [
    {
      "severity""information",
      "code""informational",
      "diagnostics""Successfully deleted 1 resource(s) in 20ms"
    }
  ]
}

Few other observations:
====================
When I query my Patient resource using the Patient identifier it says that my resources was deleted
{
  "resourceType""OperationOutcome",
  "text": {
    "status""generated",
    "div""<div xmlns=\"http://www.w3.org/1999/xhtml\"><h1>Operation Outcome</h1><table border=\"0\"><tr><td style=\"font-weight: bold;\">error</td><td>[]</td><td><pre>Resource was deleted at 2020-02-20T21:49:59.585+00:00</pre></td>\n\t\t\t\t\t\n\t\t\t\t\n\t\t\t</tr>\n\t\t</table>\n\t</div>"
  },
  "issue": [
    {
      "severity""error",
      "code""processing",
      "diagnostics""Resource was deleted at 2020-02-20T21:49:59.585+00:00"
    }
  ]
}

When I query MedicationAdministration passing the Patient identifier it returns the following message implying that while Patient was deleted but its reference still
exists somewhere because of which the child MedicationAdmninistration resource has no issue existing


{
  "resourceType""OperationOutcome",
  "text": {
    "status""generated",
    "div""<div xmlns=\"http://www.w3.org/1999/xhtml\"><h1>Operation Outcome</h1><table border=\"0\"><tr><td style=\"font-weight: bold;\">error</td><td>[]</td><td><pre>Resource with ID 265411 exists but it is not of type MedicationAdministration, found resource of type Patient</pre></td>\n\t\t\t\t\t\n\t\t\t\t\n\t\t\t</tr>\n\t\t</table>\n\t</div>"
  },
  "issue": [
    {
      "severity""error",
      "code""processing",
      "diagnostics""Resource with ID 265411 exists but it is not of type MedicationAdministration, found resource of type Patient"
    }
  ]
}

Am I doing something wrong? How can I fix my issue?

Thanks
Vikas

James Agnew

unread,
Feb 20, 2020, 6:17:09 PM2/20/20
to goods...@gmail.com, HAPI FHIR
Was resource 265411 a Patient or a MedicationAdministration before you did the delete? If it was a Patient, then this is the expected behaviour. In HAPI FHIR you can't have two resources with the same numeric ID.

If 265411 was a MedicationAdministration before you did the delete then this seems very weird...

Cheers,
James

--
You received this message because you are subscribed to the Google Groups "HAPI FHIR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hapi-fhir+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hapi-fhir/aae0a17b-b079-489e-90ae-f29fce59e98c%40googlegroups.com.

goods...@gmail.com

unread,
Feb 20, 2020, 6:29:41 PM2/20/20
to HAPI FHIR
265411 was the Patient identifier which I deleted expecting its children "MedicationAdmninistration" resources will also get deleted. That is my first issue (why did cascade deletion not happen?

My second issue is, how was only the Patient resource deleted and why it did not reject the deletion because it was leaving orphan MedicationAdministration resources? The reason I queried MedicationAdministration with the Patient identifier was because I wanted to see the behavior of the system WRT to the Patient identifier after its deletion.  It seems system still knows that there is/was a patient with that ID and, based on the error message, I am assuming is why it did not complain about orphan MedicationAdministration records and allowed the deletion of Patient.

Thanks
Vikas
To unsubscribe from this group and stop receiving emails from it, send an email to hapi...@googlegroups.com.

James Agnew

unread,
Feb 20, 2020, 6:32:49 PM2/20/20
to goods...@gmail.com, HAPI FHIR
I'm sorry, I'm not understanding your issue. It might be easiest for you to demonstrate it on our public test server ( hapi.fhir.org ).

Can you please create your pair of resources there, and see if you experience the same issue? If you do, can you link to the two resources and paste them here?

Cheees,
James

To unsubscribe from this group and stop receiving emails from it, send an email to hapi-fhir+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hapi-fhir/217eed21-080f-47ba-bc37-b8ef8be7adf1%40googlegroups.com.

goods...@gmail.com

unread,
Feb 20, 2020, 8:29:47 PM2/20/20
to HAPI FHIR
Hi James

I am not able to access the Test server. Getting a 502 Bad Gateway error. So let me try to explain what I did step by step. This will explain things better

Step 1
=====
updated the HAPI.PROPERTIES File as mentioned below to allow cascade deletion. After making this change, generated the WAR file and deployed it on the server

cascading_delete.enabled=true

Step 2
=====
Created a Patient resource. It created one with ID 265411

Step 3
=====
Created 2 Medication administration resources for the above Patient 265411. The IDs of the 2 MedicationAdministration resources created were 265412 and 265413

Step 4
=====
Invoked the DELETE API by using the following request (used POSTMAN)

It deleted the Patient resource with ID 265411. This was confirmed by the DELETE API response. It DID NOT delete the 2 MedicationAdministration resources as there was no mention of that in the JSON response.

Step 5
=====
I confirmed the deletion of Patient 265411 by trying to retrieve it. The response came back indicating that this ID does not exist and was deleted.
I tried to retrieve the 2 MedicationAdministration resources using their respective IDs 265412 and 265413. I was able to retrieve both of them


My first issue
===========
Why did the cascade deletion not happen in Step 4? I was expecting in Step 4 the 2 MedicationAdministration resources to also get deleted. How can I resolve my issue and make cascade deletion work where the Patient gets deleted along with its child MedicationAdministration resources?

Please also confirm if my expected behavior below from the system is correct or not
=========================================================================
If cascade deletion is not working, I expect the Patient deletion without the deletion of MedicationAdministration resources to fail since the system should prevent orphan data to be left in the repository. But Step 4 deleted the Patient leaving the orphan MedicationAdministration resources. Isn't this an issue?

Hope I am able to do a better job explaining my issue this time. Appreciate your help looking into my issue.

Thanks again
Vikas

James Agnew

unread,
Feb 20, 2020, 9:05:50 PM2/20/20
to goods...@gmail.com, HAPI FHIR
Apologies- The public server was down. I have restored it now.

To unsubscribe from this group and stop receiving emails from it, send an email to hapi-fhir+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hapi-fhir/0a5ca11b-4d26-46ab-8e74-59146aca8197%40googlegroups.com.

goods...@gmail.com

unread,
Feb 21, 2020, 10:35:18 AM2/21/20
to HAPI FHIR
Hi James,
I performed 2 tests as indicated below and in both my tests cascade deletion did not work. I assumed that this server has cascade deletion enabled

Test1:
=====
Created Patient. identifier of this Patient was 625304
Created 3 Med Admin for the above Patient. Identifiers of Med Admin were 625306, 625307, 625308
Deleted Patient 625304 using http://hapi.fhir.org/baseR4/Patient/625309?_cascade=true
Only Patient 625304 got deleted. The above 3 Med Admin resources 625306, 625307, 625308 did not get deleted. No Error was returned in response

Test2:
=====
Created Patient. identifier of this Patient was 625309
Created 2 Med Admin for the above Patient. Identifiers of Med Admin were 625310, 625311
Deleted Patient 625309 using http://hapi.fhir.org/baseR4/Patient/625309?_cascade=delete
Only Patient 625309 got deleted. The above 2 Med Admin resources 625310, 625311 did not get deleted. No Error was returned in response

Thanks
Vikas

James Agnew

unread,
Feb 21, 2020, 5:21:02 PM2/21/20
to goods...@gmail.com, HAPI FHIR
Your medication administration doesn't actually have any reference to the patient?


Cheers,
James

To unsubscribe from this group and stop receiving emails from it, send an email to hapi-fhir+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hapi-fhir/dc7b4160-ef8f-4ecb-890e-c374b7e89d07%40googlegroups.com.

goods...@gmail.com

unread,
Feb 25, 2020, 9:46:56 AM2/25/20
to HAPI FHIR
Hi James,
I noticed that the MedicationAdministration resources that I created did not have the reference to the Patient when I created them. That was because I was not using element name "subject"  to define the Patient reference as required in R4. I had used element name "patient" as required in DSTU2. Once I fixed this I was able to create a Patient (626955) with 2 MedicationAdministration resources (626969, 626970) and then perform the cascade deletion of my Patient which also deleted my 2 MedicationAdministration  resources along with the Patient resource. So this feature is working in the HAPI FHIR test server.
So my question is why would it not work in my environment? Please note that my environment is setup with DSTU2 configuration and the references are set correctly in my MedicationAdministration resources.
Thanks
Vikas

James Agnew

unread,
Feb 26, 2020, 9:49:16 AM2/26/20
to goods...@gmail.com, HAPI FHIR
Are you using the latest git master of the starter project? If so, tt mirrors pretty much exactly the code that is up on hapi.fhir.org so presumably there must be something different going on here.. hard to guess what though.

Sounds like your test on hapi.fhir.org wasn't using the same version of FHIR as your local test, might want to start there..

To unsubscribe from this group and stop receiving emails from it, send an email to hapi-fhir+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hapi-fhir/ee1e9afc-a676-489e-b5fa-7d9fb90bda0a%40googlegroups.com.

Blake Mumford

unread,
Jul 10, 2020, 4:40:19 AM7/10/20
to HAPI FHIR
Hi,

I had this exact same issue with a fairly similar set up. I tried changing database as well FWIW to MySQL but didn't make any difference.

To get cascade deletes working for hapi-fhir-jpaserver-starter project I had to do the following:
  • Allow CORS "x-cascade" header if desired by adding config.addAllowedHeader("x-cascade"); to FhirServerConfigCommon.java where CORS configuration is done.
  • In hapi.properties there is a property called "enforce_referential_integrity_on_delete" which by default is set to false. This should be true if you want to enable cascade deletes.
I don't know whether this is the intended behaviour/configuration, but I wasted a significant amount of time before I worked this out. It was particularly confusing as allow_cascading_deletes defaults to true, but then it won't work out of the box without changing "enforce_referential_integrity_on_delete" to true. I think it should probably be included in the documentation for cascade deletes.

Kind regards,

Blake Mumford
Reply all
Reply to author
Forward
0 new messages