Merge pull request #56620 from mihir-kandoi/pg-orderby-limit1-tiebreakers

fix: deterministic tiebreakers for ORDER BY <date> DESC LIMIT 1 lookups (MariaDB↔Postgres parity)
This commit is contained in:
Mihir Kandoi
2026-06-29 22:18:04 +05:30
committed by GitHub
4 changed files with 9 additions and 1 deletions

View File

@@ -601,6 +601,7 @@ def calculate_exchange_rate_using_last_gle(company, account, party_type, party):
.select(gl.voucher_type, gl.voucher_no)
.where(Criterion.all(conditions))
.orderby(gl.posting_date, order=Order.desc)
.orderby(gl.name, order=Order.desc)
.limit(1)
.run()[0]
)
@@ -615,6 +616,7 @@ def calculate_exchange_rate_using_last_gle(company, account, party_type, party):
(gl.voucher_type == voucher_type) & (gl.voucher_no == voucher_no) & (gl.account == account)
)
.orderby(gl.posting_date, order=Order.desc)
.orderby(gl.name, order=Order.desc)
.limit(1)
.run()[0][0]
)

View File

@@ -895,7 +895,11 @@ class GrossProfitGenerator:
if row.cost_center:
query = query.where(purchase_invoice_item.cost_center == row.cost_center)
query = query.orderby(purchase_invoice.posting_date, order=frappe.qb.desc).limit(1)
query = (
query.orderby(purchase_invoice.posting_date, order=frappe.qb.desc)
.orderby(purchase_invoice.name, order=frappe.qb.desc)
.limit(1)
)
last_purchase_rate = query.run()
return flt(last_purchase_rate[0][0]) if last_purchase_rate else 0

View File

@@ -139,6 +139,7 @@ class AssetMovement(Document):
.select(asm_item.target_location, asm_item.to_employee)
.where((asm_item.asset == asset) & (asm.company == self.company) & (asm.docstatus == 1))
.orderby(asm.transaction_date, order=frappe.qb.desc)
.orderby(asm.name, order=frappe.qb.desc)
.limit(1)
.run()
)

View File

@@ -86,6 +86,7 @@ def get_last_sales_amt(customer, doctype):
.select(sales_doctype.base_net_total)
.where((sales_doctype.customer == customer) & (sales_doctype.docstatus == 1))
.orderby(date_col, order=frappe.qb.desc)
.orderby(sales_doctype.name, order=frappe.qb.desc)
.limit(1)
).run()