8000 fix: delete existing children first to avoid `UniqueValidationError` by sagarvora · Pull Request #24140 · frappe/frappe · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: delete existing children first to avoid UniqueValidationError #24140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions frappe/model/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,36 +420,35 @@ def update_children(self):

def update_child_table(self, fieldname: str, df: Optional["DocField"] = None):
"""sync child table for given fieldname"""
rows = []
df: "DocField" = df or self.meta.get_field(fieldname)
all_rows = self.get(df.fieldname)

for d in self.get(df.fieldname):
d: Document
d.db_update()
rows.append(d.name)

if (
df.options in (self.flags.ignore_children_type or [])
# delete rows that do not match the ones in the document
# if the doctype isn't in ignore_children_type flag and isn't virtual
if not (
df.options in (self.flags.ignore_children_type or ())
or frappe.get_meta(df.options).is_virtual == 1
):
# do not delete rows for this because of flags
# hack for docperm :(
return
existing_row_names = [row.name for row in all_rows if row.name and not row.is_new()]

tbl = frappe.qb.DocType(df.options)
qry = (
frappe.qb.from_(tbl)
.where(tbl.parent == self.name)
.where(tbl.parenttype == self.doctype)
.where(tbl.parentfield == fieldname)
.delete()
)

# delete rows that do not match the ones in the document
tbl = frappe.qb.DocType(df.options)
qry = (
frappe.qb.from_(tbl)
.where(tbl.parent == self.name)
.where(tbl.parenttype == self.doctype)
.where(tbl.parentfield == fieldname)
.delete()
)
if existing_row_names:
qry = qry.where(tbl.name.notin(existing_row_names))

if rows:
qry = qry.where(tbl.name.notin(rows))
qry.run()

qry.run()
# update / insert
for d in all_rows:
d: Document
d.db_update()

def get_doc_before_save(self) -> "Document":
return getattr(self, "_doc_before_save", None)
Expand Down
0