diff --git a/frappe/database/database.py b/frappe/database/database.py index 816d91062c..e11e2fccbd 100644 --- a/frappe/database/database.py +++ b/frappe/database/database.py @@ -987,7 +987,6 @@ class Database: self.after_commit.run() self.flush_realtime_log() - flush_local_link_count() def rollback(self, *, save_point=None): """`ROLLBACK` current transaction. Optionally rollback to a known save_point.""" diff --git a/frappe/model/utils/link_count.py b/frappe/model/utils/link_count.py index 9a7694b9f8..3218d8482a 100644 --- a/frappe/model/utils/link_count.py +++ b/frappe/model/utils/link_count.py @@ -14,6 +14,8 @@ def notify_link_count(doctype, name): else: frappe.local.link_count[(doctype, name)] = 1 + frappe.db.after_commit.add(flush_local_link_count) + def flush_local_link_count(): """flush from local before ending request""" @@ -31,6 +33,7 @@ def flush_local_link_count(): link_count[key] = frappe.local.link_count[key] frappe.cache().set_value("_link_count", link_count) + frappe.local.link_count = {} def update_link_count(): @@ -38,14 +41,12 @@ def update_link_count(): link_count = frappe.cache().get_value("_link_count") if link_count: - for key, count in link_count.items(): - if key[0] not in ignore_doctypes: + for (doctype, name), count in link_count.items(): + if doctype not in ignore_doctypes: try: - frappe.db.sql( - f"update `tab{key[0]}` set idx = idx + {count} where name=%s", - key[1], - auto_commit=1, - ) + table = frappe.qb.DocType(doctype) + frappe.qb.update(table).set(table.idx, table.idx + count).where(table.name == name).run() + frappe.db.commit() except Exception as e: if not frappe.db.is_table_missing(e): # table not found, single raise e