From e47b03af692a6c090eb502e3f70540f8e2d098cd Mon Sep 17 00:00:00 2001 From: Sachin Mane Date: Fri, 15 Jun 2018 09:03:22 +0530 Subject: [PATCH] Automatically create unique index on autoname field (#5674) * Automatically create unique index on autoname field * Update doctype.py * Indentation fix. --- frappe/core/doctype/doctype/doctype.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/frappe/core/doctype/doctype/doctype.py b/frappe/core/doctype/doctype/doctype.py index 1190e85ca5..dfd8002e08 100644 --- a/frappe/core/doctype/doctype/doctype.py +++ b/frappe/core/doctype/doctype/doctype.py @@ -196,11 +196,17 @@ class DocType(Document): self.autoname = "naming_series:" # validate field name if autoname field:fieldname is used - + # Create unique index on autoname field automatically. if autoname and autoname.startswith('field:'): field = autoname.split(":")[1] if not field or field not in [ df.fieldname for df in self.fields ]: frappe.throw(_("Invalid fieldname '{0}' in autoname".format(field))) + else: + for df in self.fields: + if df.fieldname == field: + df.search_index = 1 + df.unique = 1 + break if autoname and (not autoname.startswith('field:')) \ and (not autoname.startswith('eval:')) \ @@ -835,4 +841,4 @@ def check_if_fieldname_conflicts_with_methods(doctype, fieldname): frappe.throw(_("Fieldname {0} conflicting with meta object").format(fieldname)) def clear_linked_doctype_cache(): - frappe.cache().delete_value('linked_doctypes_without_ignore_user_permissions_enabled') \ No newline at end of file + frappe.cache().delete_value('linked_doctypes_without_ignore_user_permissions_enabled')