diff --git a/frappe/database/schema.py b/frappe/database/schema.py index 647b28fb49..35aa1e6abf 100644 --- a/frappe/database/schema.py +++ b/frappe/database/schema.py @@ -443,6 +443,9 @@ def get_definition(fieldtype, precision=None, length=None, *, options=None): if length: if coltype == "varchar": + # Reference: https://mariadb.com/docs/server/server-usage/storage-engines/innodb/innodb-row-formats/troubleshooting-row-size-too-large-errors-with-innodb + if length < 64: + length = 64 size = length elif coltype == "int" and length < 11: # allow setting custom length for int if length provided is less than 11 diff --git a/frappe/tests/test_db_update.py b/frappe/tests/test_db_update.py index dc150b1be8..ec2cb423b7 100644 --- a/frappe/tests/test_db_update.py +++ b/frappe/tests/test_db_update.py @@ -176,6 +176,18 @@ class TestDBUpdate(IntegrationTestCase): self.assertEqual(frappe.db.get_column_type(referring_doctype.name, link), "uuid") + @run_only_if(db_type_is.MARIADB) + def test_varchar_length(self): + from frappe.database.schema import add_column + + test_doc = new_doctype().insert() + col_name = f"col_{frappe.generate_hash(length=4)}" + add_column(test_doc.name, fieldtype="Data", column_name=col_name, length=50) + length = frappe.db.sql( + f"SELECT CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tab{test_doc.name}' AND COLUMN_NAME = '{col_name}' ", + )[0][0] + self.assertEqual(length, 64) + class TestDBUpdateSanityChecks(IntegrationTestCase): @run_only_if(db_type_is.MARIADB)