Error:Too many changes to database in single action when upgrading from V12 to V13

Hi all,
I am currently migrating a Customer from V12 to V13 and this customer uses Healthcare module.

It has 92672 records on tabPatient Encounter.
When running Migrate the error “Too many changes to database in single action.
The changes have been reverted.
”.
This error happens on erpnext.patches.v12_0.update_healthcare_refactored_changes

Tried to split the patch like
for entry in encounter_details:

        doc = frappe.get_doc("Patient Encounter", entry.name)

        symptoms = entry.symptoms.split("\n") if entry.symptoms else []

        for symptom in symptoms:

            if not frappe.db.exists("Complaint", symptom):

                frappe.get_doc({"doctype": "Complaint", "complaints": symptom}).insert()

            row = doc.append("symptoms", {"complaint": symptom})

            row.db_update()

        doc.db_update()

   

    #HELKYDS 09-10-2022; To avoid Error: Too many changes to database in single action

    print ('Completed Complaints...')

    for entry in encounter_details:

        doc = frappe.get_doc("Patient Encounter", entry.name)

        diagnosis = entry.diagnosis.split("\n") if entry.diagnosis else []

        for d in diagnosis:

            if not frappe.db.exists("Diagnosis", d):

                frappe.get_doc({"doctype": "Diagnosis", "diagnosis": d}).insert()

            row = doc.append("diagnosis", {"diagnosis": d})

            row.db_update()

        doc.db_update()

But still receiving the error when running the second part of the Code…

File “/home/frappe/frappe-bench/apps/erpnext/erpnext/patches/v12_0/update_healthcare_refactored_changes.py”, line 122, in execute
row.db_update()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/base_document.py”, line 438, in db_update
self.db_insert()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/base_document.py”, line 400, in db_insert
frappe.db.sql(
File “/home/frappe/frappe-bench/apps/frappe/frappe/database/database.py”, line 151, in sql
self.check_transaction_status(query)
File “/home/frappe/frappe-bench/apps/frappe/frappe/database/database.py”, line 312, in check_transaction_status
raise frappe.TooManyWritesError(msg)
frappe.exceptions.TooManyWritesError:

Too many changes to database in single action.
The changes have been reverted.

Any Solution is welcome.

My Temp solution was to duplicate the file erpnext.patches.v12_0.update_healthcare_refactored_changes to erpnext.patches.v12_0.update_healthcare_refactored_changes_1 and add on patches.txt of ERPNEXT.
Now on original file ( erpnext.patches.v12_0.update_healthcare_refactored_changes) removed the code to the other file duplicated
for entry in encounter_details:

        doc = frappe.get_doc("Patient Encounter", entry.name)

        diagnosis = entry.diagnosis.split("\n") if entry.diagnosis else []

        for d in diagnosis:

            if not frappe.db.exists("Diagnosis", d):

                frappe.get_doc({"doctype": "Diagnosis", "diagnosis": d}).insert()

            row = doc.append("diagnosis", {"diagnosis": d})

            row.db_update()

        doc.db_update()

After doing those changes and running migrate… all goes well.

Hope a better solution can be found for DBs with more or same Number of Records i have.
:slight_smile: