Repository: frappe/erpnext
Pull request: on delete contact update issue
fixes https://github.com/frappe/frappe/issues/5724

Problem statement:
Missing tabIssue
Bug report

I installed only frappe without erpnext and created a new Contact. When I tried to delete it I got the error stated below. The error appears every time when deleting Contact. The operation needs table "Issue" which is in erpnext. The error does not appear when the erpnext is installed on the site.

Steps to reproduce:
Install frappe only
Go to Contact List
Create new Contact
Delete the Contact

Error:
-----------------------------------------------------------------------------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 62, in application
    response = frappe.handler.handle()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 22, in handle
    data = execute_cmd(cmd)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 53, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 942, in call
    return fn(*args, **newargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/client.py", line 237, in delete
    frappe.delete_doc(doctype, name, ignore_missing=False)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 676, in delete_doc
    ignore_permissions, flags, ignore_on_trash, ignore_missing)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/delete_doc.py", line 78, in delete_doc
    doc.run_method("on_trash")
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 765, in run_method
    out = Document.hook(fn)(self, *args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 1040, in composer
    return composed(self, method, *args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 1023, in runner
    add_to_return_value(self, fn(self, *args, **kwargs))
  File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 759, in <lambda>
    fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/contacts/doctype/contact/contact.py", line 45, in on_trash
    self.name)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/database.py", line 199, in sql
    self._cursor.execute(query, values)
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/cursors.py", line 165, in execute
    result = self._query(query)
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/cursors.py", line 321, in _query
    conn.query(q)
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/connections.py", line 860, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/connections.py", line 1061, in _read_query_result
    result.read()
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/connections.py", line 1349, in read
    first_packet = self.connection._read_packet()
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/connections.py", line 1018, in _read_packet
    packet.check_error()
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/connections.py", line 384, in check_error
    err.raise_mysql_exception(self._data)
  File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/pymysql/err.py", line 107, in raise_mysql_exception
    raise errorclass(errno, errval)
ProgrammingError: (1146, u"Table 'a7708dafc596f3dc.tabIssue' doesn't exist")

Hints from issue discussion:

Commit message: on delete contact update isuue

remove contact from issue if the deleted contact exists

Patch to review:
diff --git a/erpnext/custom_hooks/__init__.py b/erpnext/custom_hooks/__init__.py
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/erpnext/custom_hooks/contacts.py b/erpnext/custom_hooks/contacts.py
new file mode 100644
index 000000000000..e1db33de288a
--- /dev/null
+++ b/erpnext/custom_hooks/contacts.py
@@ -0,0 +1,11 @@
+#!/bin/env python
+# -*- coding: utf-8 -*-
+
+"""Update issue."""
+import frappe
+
+
+def update_issue(contact, method):
+	"""Update tabIssue"""
+	frappe.db.sql("""update `tabIssue` set contact='' where contact=%s""",	
+-			contact.name)
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index 5f1e7d4243c5..c7bf641f9c77 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -206,6 +206,9 @@
 	},
 	('Sales Invoice', 'Purchase Invoice', 'Delivery Note'): {
 		'validate': 'erpnext.regional.india.utils.set_place_of_supply'
+	},
+	"Contact":{
+		"on_trash": "erpnext.custom_hooks.contacts.update_issue"
 	}
 }
 


Code review comments:
diff_hunk: @@ -0,0 +1,11 @@
+#!/bin/env python
path: erpnext/custom_hooks/contacts.py
text: @user1:
put this in `issue.py`