From c230e86e2c144f0a539bcf151197f60a3eb5ae00 Mon Sep 17 00:00:00 2001 From: Hussain Nagaria Date: Wed, 17 Sep 2025 13:27:13 +0530 Subject: [PATCH] fix: handle length more robustly --- frappe/core/doctype/docfield/docfield.json | 6 +++--- frappe/core/doctype/doctype/doctype.py | 17 +++++++++++++++++ frappe/database/schema.py | 6 ++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/frappe/core/doctype/docfield/docfield.json b/frappe/core/doctype/docfield/docfield.json index 6583cf8e91..92d48fa182 100644 --- a/frappe/core/doctype/docfield/docfield.json +++ b/frappe/core/doctype/docfield/docfield.json @@ -11,8 +11,8 @@ "label", "fieldtype", "fieldname", - "precision", "length", + "precision", "non_negative", "hide_days", "hide_seconds", @@ -135,7 +135,7 @@ }, { "depends_on": "eval:in_list([\"Float\", \"Currency\", \"Percent\"], doc.fieldtype)", - "description": "Set non-standard precision for a Float or Currency field", + "description": "Set non-standard precision for a Float, Currency or Percent field", "fieldname": "precision", "fieldtype": "Select", "label": "Precision", @@ -614,7 +614,7 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2025-09-03 14:40:20.813091", + "modified": "2025-09-17 13:20:57.852396", "modified_by": "Administrator", "module": "Core", "name": "DocField", diff --git a/frappe/core/doctype/doctype/doctype.py b/frappe/core/doctype/doctype/doctype.py index 845dbf5219..69c76298ec 100644 --- a/frappe/core/doctype/doctype/doctype.py +++ b/frappe/core/doctype/doctype/doctype.py @@ -1660,6 +1660,22 @@ def validate_fields(meta: Meta): if docfield.options and (int(docfield.options) > 10 or int(docfield.options) < 3): frappe.throw(_("Options for Rating field can range from 3 to 10")) + def check_decimal_config(docfield): + if docfield.fieldtype not in ("Currency", "Float", "Percent"): + return + + if docfield.length and docfield.precision: + if cint(docfield.precision) > cint(docfield.length): + frappe.throw( + _("Precision ({0}) for {1} cannot be greater than its length ({2}).").format( + docfield.precision, frappe.bold(docfield.label), docfield.length + ) + ) + + precision_not_set = docfield.precision in (None, "") + if docfield.length and precision_not_set and cint(docfield.length) < 9: # default is 9 + docfield.precision = docfield.length + fields = meta.get("fields") fieldname_list = [d.fieldname for d in fields] @@ -1682,6 +1698,7 @@ def validate_fields(meta: Meta): scrub_options_in_select(d) validate_fetch_from(d) validate_data_field_type(d) + check_decimal_config(d) if not frappe.flags.in_migrate or in_ci: check_unique_fieldname(meta.get("name"), d.fieldname) diff --git a/frappe/database/schema.py b/frappe/database/schema.py index f1f18fd43b..f48a6e1fc9 100644 --- a/frappe/database/schema.py +++ b/frappe/database/schema.py @@ -432,7 +432,7 @@ def get_definition(fieldtype, precision=None, length=None, *, options=None): # This check needs to exist for backward compatibility. # Till V13, default size used for float, currency and percent are (18, 6). if fieldtype in ["Float", "Currency", "Percent"] and cint(precision) > 6: - size = "21,9" + size = f"21,{cint(precision)}" if length: if coltype == "varchar": @@ -443,7 +443,9 @@ def get_definition(fieldtype, precision=None, length=None, *, options=None): # in postgres as bigint (as seen in type_map) size = length elif coltype == "decimal": - size = f"{length},9" + max_possible_precision = min(9, length) + precision = max_possible_precision if precision in (None, "") else cint(precision) + size = f"{length},{precision}" if size is not None: coltype = f"{coltype}({size})"