Error : Lead Source : Options must be a valid DocType for field Source

I’m getting an error on a site which has custom fields on “Lead”, “Sales Order” and “Opportunity”

bench migrate
Migrating test_awfis.com
Updating frappe                     : [========================================]
Updating erpnext                    : [========================================]
Updating awfis_erpnext              : [========================================]
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/revant/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 79, in <module>
    main()
  File "/home/revant/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 16, in main
    click.Group(commands=commands)(prog_name='bench')
  File "/home/revant/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py", line 716, in __call__
    return self.main(*args, **kwargs)
  File "/home/revant/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py", line 696, in main
    rv = self.invoke(ctx)
  File "/home/revant/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py", line 1060, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/revant/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py", line 1060, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/revant/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py", line 889, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/revant/frappe-bench/env/local/lib/python2.7/site-packages/click/core.py", line 534, in invoke
    return callback(*args, **kwargs)
  File "/home/revant/frappe-bench/env/local/lib/python2.7/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/revant/frappe-bench/apps/frappe/frappe/commands/__init__.py", line 24, in _func
    ret = f(frappe._dict(ctx.obj), *args, **kwargs)
  File "/home/revant/frappe-bench/apps/frappe/frappe/commands/site.py", line 209, in migrate
    migrate(context.verbose, rebuild_website=rebuild_website)
  File "/home/revant/frappe-bench/apps/frappe/frappe/migrate.py", line 34, in migrate
    sync_fixtures()
  File "/home/revant/frappe-bench/apps/frappe/frappe/utils/fixtures.py", line 24, in sync_fixtures
    ignore_links=True, overwrite=True)
  File "/home/revant/frappe-bench/apps/frappe/frappe/core/page/data_import_tool/data_import_tool.py", line 95, in import_doc
    frappe.modules.import_file.import_file_by_path(f, data_import=True, force=True, pre_process=pre_process)
  File "/home/revant/frappe-bench/apps/frappe/frappe/modules/import_file.py", line 54, in import_file_by_path
    import_doc(doc, force=force, data_import=data_import, pre_process=pre_process)
  File "/home/revant/frappe-bench/apps/frappe/frappe/modules/import_file.py", line 122, in import_doc
    doc.insert()
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 222, in insert
    self.run_post_save_methods()
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 771, in run_post_save_methods
    self.run_method("on_update")
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 654, in run_method
    out = Document.hook(fn)(self, *args, **kwargs)
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 882, in composer
    return composed(self, method, *args, **kwargs)
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 865, in runner
    add_to_return_value(self, fn(self, *args, **kwargs))
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 648, in <lambda>
    fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
  File "/home/revant/frappe-bench/apps/frappe/frappe/custom/doctype/custom_field/custom_field.py", line 46, in on_update
    validate_fields_for_doctype(self.dt)
  File "/home/revant/frappe-bench/apps/frappe/frappe/core/doctype/doctype/doctype.py", line 264, in validate_fields_for_doctype
    validate_fields(frappe.get_meta(doctype, cached=False))
  File "/home/revant/frappe-bench/apps/frappe/frappe/core/doctype/doctype/doctype.py", line 462, in validate_fields
    check_link_table_options(d)
  File "/home/revant/frappe-bench/apps/frappe/frappe/core/doctype/doctype/doctype.py", line 306, in check_link_table_options
    frappe.throw(_("Options must be a valid DocType for field {0} in row {1}").format(d.label, d.idx))
  File "/home/revant/frappe-bench/apps/frappe/frappe/__init__.py", line 300, in throw
    msgprint(msg, raise_exception=exc, title=title, indicator='red')
  File "/home/revant/frappe-bench/apps/frappe/frappe/__init__.py", line 293, in msgprint
    _raise_exception()
  File "/home/revant/frappe-bench/apps/frappe/frappe/__init__.py", line 266, in _raise_exception
    raise raise_exception, encode(msg)
frappe.exceptions.ValidationError: Options must be a valid DocType for field Source in row 89

All custom Fields

In [2]: frappe.get_all("Custom Field")
Out[2]: 
[{'name': u'Communication-awfis_channel'},
 {'name': u'Issue-awfis_channel'},
 {'name': u'Purchase Order-awfis_warehouse'},
 {'name': u'Item Group-expiry_warning_period'},
 {'name': u'Item Attribute Value-col_break_1'},
 {'name': u'Item Attribute Value-awfis_centre'},
 {'name': u'Customer-company_website'},
 {'name': u'Customer-industry'},
 {'name': u'Customer-number_of_employees'},
 {'name': u'Contact-contact_type'},
 {'name': u'Contact-col_break_custom_1'},
 {'name': u'Contact-date_of_birth'},
 {'name': u'Contact-date_of_anniversary'},
 {'name': u'Contact-pan_card_no'},
 {'name': u'Contact-joining_date'},
 {'name': u'Sales Order-awfis_lead'},
 {'name': u'Sales Order-awfis_lead_source'},
 {'name': u'Sales Order-awfis_channel_partner'},
 {'name': u'Sales Order-section_city_centre'},
 {'name': u'Sales Order-awfis_city'},
 {'name': u'Sales Order-column_break_city_centre'},
 {'name': u'Sales Order-centre'},
 {'name': u'Sales Order-discount_coupon'},
 {'name': u'Sales Order-mode_of_payment'},
 {'name': u'Sales Order-awfis_booking_id'},
 {'name': u'Opportunity-awfis_lead_channel'},
 {'name': u'Opportunity-awfis_lead_territory'},
 {'name': u'Opportunity-reason_lost_or_on_hold'},
 {'name': u'Opportunity-opportunity_awfis_centres'},
 {'name': u'Opportunity-complaint_issue'},
 {'name': u'Opportunity-priority'},
 {'name': u'Lead-first_name'},
 {'name': u'Lead-last_name'},
 {'name': u'Lead-awfis_mobile_no'},
 {'name': u'Lead-awfis_lead_territory'},
 {'name': u'Lead-awfis_email_id'},
 {'name': u'Lead-awfis_company_website'},
 {'name': u'Lead-site_visited'},
 {'name': u'Lead-lead_state'},
 {'name': u'Lead-reason_lost_or_on_hold'},
 {'name': u'Lead-social_media'},
 {'name': u'Lead-online_listing'},
 {'name': u'Lead-channel_partner'},
 {'name': u'Lead-awfis_lead_channel'},
 {'name': u'Lead-section_break_centres'},
 {'name': u'Lead-lead_awfis_centres'},
 {'name': u'Lead-sb_spaces'},
 {'name': u'Lead-awfis_spaces'},
 {'name': u'Print Settings-compact_item_print'},
 {'name': u'Warehouse-awfis_warehouse_territory'},
 {'name': u'Item Group-sb1_expiry_warning_period'}]

Custom Fields causing error on save:

In [1]: for i in frappe.get_all("Custom Field"):
   ...:     try:
   ...:         frappe.get_doc("Custom Field", i.name).save()
   ...:     except:
   ...:         print i.name
   ...:         
Lead-awfis_spaces
Lead-sb_spaces
Lead-lead_awfis_centres
Lead-section_break_centres
Lead-awfis_lead_channel
Lead-channel_partner
Lead-online_listing
Lead-social_media
Lead-reason_lost_or_on_hold
Lead-lead_state
Lead-site_visited
Lead-awfis_company_website
Lead-awfis_email_id
Lead-awfis_lead_territory
Lead-awfis_mobile_no
Lead-last_name
Lead-first_name
Opportunity-priority
Opportunity-complaint_issue
Opportunity-opportunity_awfis_centres
Opportunity-reason_lost_or_on_hold
Opportunity-awfis_lead_territory
Opportunity-awfis_lead_channel
Sales Order-awfis_booking_id
Sales Order-mode_of_payment
Sales Order-discount_coupon
Sales Order-centre
Sales Order-column_break_city_centre
Sales Order-awfis_city
Sales Order-section_city_centre
Sales Order-awfis_channel_partner
Sales Order-awfis_lead_source
Sales Order-awfis_lead

Error on save:

In [4]: frappe.get_doc(“Custom Field”, “Lead-awfis_spaces”).save()

ValidationError Traceback (most recent call last)
/home/revant/frappe-bench/apps/frappe/frappe/commands/utils.pyc in ()
----> 1 frappe.get_doc(“Custom Field”, “Lead-awfis_spaces”).save()

/home/revant/frappe-bench/apps/frappe/frappe/model/document.pyc in save(self, *args, **kwargs)
231 def save(self, *args, **kwargs):
232 “”“Wrapper for _save”""
–> 233 return self._save(*args, **kwargs)
234
235 def _save(self, ignore_permissions=None):

/home/revant/frappe-bench/apps/frappe/frappe/model/document.pyc in _save(self, ignore_permissions)
277
278 self.update_children()
–> 279 self.run_post_save_methods()
280
281 return self

/home/revant/frappe-bench/apps/frappe/frappe/model/document.pyc in run_post_save_methods(self)
769 - update_after_submit for Update after Submit"""
770 if self._action==“save”:
–> 771 self.run_method(“on_update”)
772 elif self._action==“submit”:
773 self.run_method(“on_update”)

/home/revant/frappe-bench/apps/frappe/frappe/model/document.pyc in run_method(self, method, *args, **kwargs)
652
653 fn.name = method.encode(“utf-8”)
–> 654 out = Document.hook(fn)(self, *args, **kwargs)
655
656 self.run_email_alerts(method)

/home/revant/frappe-bench/apps/frappe/frappe/model/document.pyc in composer(self, *args, **kwargs)
880
881 composed = compose(f, *hooks)
–> 882 return composed(self, method, *args, **kwargs)
883
884 return composer

/home/revant/frappe-bench/apps/frappe/frappe/model/document.pyc in runner(self, method, *args, **kwargs)
863 def compose(fn, *hooks):
864 def runner(self, method, *args, **kwargs):
–> 865 add_to_return_value(self, fn(self, *args, **kwargs))
866 for f in hooks:
867 add_to_return_value(self, f(self, method, *args, **kwargs))

/home/revant/frappe-bench/apps/frappe/frappe/model/document.pyc in (self, *args, **kwargs)
646
647 if hasattr(self, method) and hasattr(getattr(self, method), “call”):
–> 648 fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
649 else:
650 # hack! to run hooks even if method does not exist

/home/revant/frappe-bench/apps/frappe/frappe/custom/doctype/custom_field/custom_field.pyc in on_update(self)
44 # validate field
45 from frappe.core.doctype.doctype.doctype import validate_fields_for_doctype
—> 46 validate_fields_for_doctype(self.dt)
47
48 # update the schema

/home/revant/frappe-bench/apps/frappe/frappe/core/doctype/doctype/doctype.pyc in validate_fields_for_doctype(doctype)
262
263 def validate_fields_for_doctype(doctype):
–> 264 validate_fields(frappe.get_meta(doctype, cached=False))
265
266 # this is separate because it is also called via custom field

/home/revant/frappe-bench/apps/frappe/frappe/core/doctype/doctype/doctype.pyc in validate_fields(meta)
460 check_unique_fieldname(d.fieldname)
461 check_illegal_mandatory(d)
–> 462 check_link_table_options(d)
463 check_dynamic_link_options(d)
464 check_hidden_and_mandatory(d)

/home/revant/frappe-bench/apps/frappe/frappe/core/doctype/doctype/doctype.pyc in check_link_table_options(d)
304 options = frappe.db.get_value(“DocType”, d.options, “name”)
305 if not options:
–> 306 frappe.throw(_(“Options must be a valid DocType for field {0} in row {1}”).format(d.label, d.idx))
307 else:
308 # fix case

/home/revant/frappe-bench/apps/frappe/frappe/init.pyc in throw(msg, exc, title)
298 :param msg: Message.
299 :param exc: Exception class. Default frappe.ValidationError“”"
–> 300 msgprint(msg, raise_exception=exc, title=title, indicator=‘red’)
301
302 def emit_js(js, user=False, **kwargs):

/home/revant/frappe-bench/apps/frappe/frappe/init.pyc in msgprint(msg, title, raise_exception, as_table, indicator, alert)
291
292 message_log.append(json.dumps(out))
–> 293 _raise_exception()
294
295 def throw(msg, exc=ValidationError, title=None):

/home/revant/frappe-bench/apps/frappe/frappe/init.pyc in _raise_exception()
264
265 if inspect.isclass(raise_exception) and issubclass(raise_exception, Exception):
–> 266 raise raise_exception, encode(msg)
267 else:
268 raise ValidationError, encode(msg)

ValidationError: Options must be a valid DocType for field Source in row 17

Sites without custom fields on Lead, Sales Order and Opportunity updated smoothly.

If this is on the latest version of bench update, could you raise an issue on GitHub about it?