fix: better way to extract fieldname from key_name
This commit is contained in:
parent
9656ef3940
commit
ae4e39480e
1 changed files with 40 additions and 8 deletions
|
|
@ -402,25 +402,57 @@ class BaseDocument(object):
|
|||
doc.db_update()
|
||||
|
||||
def show_unique_validation_message(self, e):
|
||||
# TODO: Find a better way to extract fieldname
|
||||
if frappe.db.db_type != 'postgres':
|
||||
fieldname = str(e).split("'")[-2]
|
||||
label = None
|
||||
|
||||
# unique_first_fieldname_second_fieldname is the constraint name
|
||||
# created using frappe.db.add_unique
|
||||
if "unique_" in fieldname:
|
||||
fieldname = fieldname.split("_", 1)[1]
|
||||
# MariaDB gives key_name in error. Extracting fieldname from key name
|
||||
try:
|
||||
fieldname = self.get_field_name_by_key_name(fieldname)
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
df = self.meta.get_field(fieldname)
|
||||
if df:
|
||||
label = df.label
|
||||
label = self.get_label_from_fieldname(fieldname)
|
||||
|
||||
frappe.msgprint(_("{0} must be unique").format(label or fieldname))
|
||||
|
||||
# this is used to preserve traceback
|
||||
raise frappe.UniqueValidationError(self.doctype, self.name, e)
|
||||
|
||||
def get_field_name_by_key_name(self, key_name):
|
||||
"""MariaDB stores a mapping between key_name and column_name.
|
||||
This function returns the column_name associated with the key_name passed
|
||||
|
||||
Args:
|
||||
key_name ([String])
|
||||
|
||||
Returns:
|
||||
[String]: [column_name associated with the String]
|
||||
"""
|
||||
return frappe.db.sql(f"""
|
||||
SHOW
|
||||
INDEX
|
||||
FROM
|
||||
`tab{self.doctype}`
|
||||
WHERE
|
||||
key_name=%s
|
||||
AND
|
||||
Non_unique=0
|
||||
""", key_name)[0][4]
|
||||
|
||||
def get_label_from_fieldname(self, fieldname):
|
||||
"""Returns the associated label for fieldname
|
||||
|
||||
Args:
|
||||
fieldname ([String])
|
||||
|
||||
Returns:
|
||||
[String]: [label associated with the String]
|
||||
"""
|
||||
df = self.meta.get_field(fieldname)
|
||||
if df:
|
||||
return df.label
|
||||
|
||||
def update_modified(self):
|
||||
"""Update modified timestamp"""
|
||||
self.set("modified", now())
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue