From acc36be87fa7b697482ad4eb787a46fef7eecfc7 Mon Sep 17 00:00:00 2001
From: Anand Doshi <anand@erpnext.com>
Date: Fri, 21 Dec 2012 20:57:03 +0530
Subject: [PATCH] patch to fix deleted contact and addresses

---
 .../december_2012/deleted_contact_address_patch.py   | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100644 patches/december_2012/deleted_contact_address_patch.py

diff --git a/patches/december_2012/deleted_contact_address_patch.py b/patches/december_2012/deleted_contact_address_patch.py
new file mode 100644
index 0000000000..3eb1b37eee
--- /dev/null
+++ b/patches/december_2012/deleted_contact_address_patch.py
@@ -0,0 +1,12 @@
+import webnotes
+def execute():
+	"""finds references of deleted addresses and contacts and deletes these references"""
+	import webnotes.model
+
+	for dt in ["Address", "Contact"]:
+		link_fields = webnotes.model.get_link_fields(dt)
+		for parent, lf in link_fields:
+			webnotes.conn.sql("""update `tab%s` ref set `%s`=null
+				where ifnull(`%s`, '')!='' and not exists (
+					select * from `tab%s` where name=ref.`%s`)""" % \
+				(parent, lf, lf, dt, lf))
\ No newline at end of file