feat: Link fields with UUID datatype in DB (#26625)

Link fields that refer to doctypes with UUID naming will also be UUID.
This commit is contained in:
Ankush Menat 2024-05-31 14:47:09 +05:30 committed by GitHub
parent 1269e31170
commit fb27368a53
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 2 deletions

View file

@ -200,7 +200,12 @@ class DbColumn:
self.not_nullable = not_nullable
def get_definition(self, for_modification=False):
column_def = get_definition(self.fieldtype, precision=self.precision, length=self.length)
column_def = get_definition(
self.fieldtype,
precision=self.precision,
length=self.length,
options=self.options,
)
if not column_def:
return column_def
@ -356,9 +361,20 @@ def validate_column_length(fieldname):
frappe.throw(_("Fieldname is limited to 64 characters ({0})").format(fieldname))
def get_definition(fieldtype, precision=None, length=None):
def get_definition(fieldtype, precision=None, length=None, *, options=None):
d = frappe.db.type_map.get(fieldtype)
if (
fieldtype == "Link"
and options
# XXX: This might not trigger if referred doctype is not yet created
# This is largely limitation of how migration happens though.
# Maybe we can sort by creation and then modified?
and frappe.db.exists("DocType", options)
and frappe.get_meta(options).autoname == "UUID"
):
d = ("uuid", None)
if not d:
return

View file

@ -163,6 +163,17 @@ class TestDBUpdate(FrappeTestCase):
self.assertIn(varchar, frappe.db.get_column_type(doctype.name, "name"))
doc.reload() # ensure that docs are still accesible
def test_uuid_link_field(self):
uuid_doctype = new_doctype().update({"autoname": "UUID"}).insert()
self.assertEqual(frappe.db.get_column_type(uuid_doctype.name, "name"), "uuid")
link = "link_field"
referring_doctype = new_doctype(
fields=[{"fieldname": link, "fieldtype": "Link", "options": uuid_doctype.name}]
).insert()
self.assertEqual(frappe.db.get_column_type(referring_doctype.name, link), "uuid")
def get_fieldtype_from_def(field_def):
fieldtuple = frappe.db.type_map.get(field_def.fieldtype, ("", 0))