fix: handle length more robustly

This commit is contained in:
Hussain Nagaria 2025-09-17 13:27:13 +05:30
parent 8f83dedbba
commit c230e86e2c
3 changed files with 24 additions and 5 deletions

View file

@ -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",

View file

@ -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)

View file

@ -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})"