Skip to content

Commit

Permalink
fix: reserved keywords as col name (#25718) (#25726)
Browse files Browse the repository at this point in the history
(cherry picked from commit 87ffe25)

Co-authored-by: Ankush Menat <ankush@frappe.io>
  • Loading branch information
mergify[bot] and ankush committed Mar 29, 2024
1 parent 2c14450 commit fca1c1a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion frappe/model/delete_doc.py
Expand Up @@ -340,7 +340,7 @@ def check_if_doc_is_dynamically_linked(doc, method="Delete"):
df["table"] = ", `parent`, `parenttype`, `idx`" if meta.istable else ""
for refdoc in frappe.db.sql(
"""select `name`, `docstatus` {table} from `tab{parent}` where
{options}=%s and {fieldname}=%s""".format(**df),
`{options}`=%s and `{fieldname}`=%s""".format(**df),
(doc.doctype, doc.name),
as_dict=True,
):
Expand Down
4 changes: 3 additions & 1 deletion frappe/model/document.py
Expand Up @@ -202,9 +202,11 @@ def load_from_db(self):
if hasattr(self, "__setup__"):
self.__setup__()

return self

def reload(self):
"""Reload document from database"""
self.load_from_db()
return self.load_from_db()

def get_latest(self):
if not getattr(self, "_doc_before_save", None):
Expand Down
2 changes: 1 addition & 1 deletion frappe/model/dynamic_links.py
Expand Up @@ -43,7 +43,7 @@ def get_dynamic_link_map(for_delete=False):
else:
try:
links = frappe.db.sql_list(
"""select distinct {options} from `tab{parent}`""".format(**df)
"""select distinct `{options}` from `tab{parent}`""".format(**df)
)
for doctype in links:
dynamic_link_map.setdefault(doctype, []).append(df)
Expand Down
37 changes: 37 additions & 0 deletions frappe/tests/test_linked_with.py
@@ -1,5 +1,9 @@
import random
import string

import frappe
from frappe.core.doctype.doctype.test_doctype import new_doctype
from frappe.database import savepoint
from frappe.desk.form import linked_with
from frappe.tests.utils import FrappeTestCase

Expand Down Expand Up @@ -148,3 +152,36 @@ def test_check_delete_integrity(self):
amendment.submit()

self.assertRaises(frappe.LinkExistsError, doc.delete)

def test_reserved_keywords(self):
dt_name = "Test " + "".join(random.sample(string.ascii_lowercase, 10))
new_doctype(
dt_name,
fields=[
{
"fieldname": "from",
"fieldtype": "Link",
"options": "DocType",
},
{
"fieldname": "order",
"fieldtype": "Dynamic Link",
"options": "from",
},
],
is_submittable=True,
).insert()

linked_doc = frappe.new_doc(dt_name).insert().submit()

second_doc = (
frappe.new_doc(dt_name, **{"from": linked_doc.doctype, "order": linked_doc.name})
.insert()
.submit()
)

with savepoint(frappe.LinkExistsError):
linked_doc.cancel() and self.fail("Cancellation shouldn't have worked")

second_doc.cancel()
linked_doc.reload().cancel()

0 comments on commit fca1c1a

Please sign in to comment.