Merge pull request #56453 from mihir-kandoi/pg-null-ordering-sentinels

fix(postgres): match MariaDB NULL ordering in the queries where it changes the result
This commit is contained in:
Mihir Kandoi
2026-06-25 14:48:41 +05:30
committed by GitHub
4 changed files with 19 additions and 12 deletions

View File

@@ -152,7 +152,7 @@ def _get_pricing_rules(apply_on, args, values):
and {child_doc}.parent = `tabPricing Rule`.name
and `tabPricing Rule`.disable = 0 and
`tabPricing Rule`.{transaction_type} = 1 {warehouse_cond} {conditions}
order by `tabPricing Rule`.priority desc,
order by coalesce(`tabPricing Rule`.priority, '') desc,
`tabPricing Rule`.name desc""".format(
child_doc=child_doc,
apply_on_field=apply_on_field,

View File

@@ -231,6 +231,7 @@ def get_items(
.where(ItemPrice.selling == 1)
.where((ItemPrice.valid_from <= current_date) | (ItemPrice.valid_from.isnull()))
.where((ItemPrice.valid_upto >= current_date) | (ItemPrice.valid_upto.isnull()))
.orderby(ItemPrice.valid_from.isnull(), order=Order.asc)
.orderby(ItemPrice.valid_from, order=Order.desc)
).run(as_dict=True)

View File

@@ -2432,6 +2432,8 @@ def get_serial_nos_based_on_filters(filters, fields, order_by, kwargs):
if kwargs.based_on == "LIFO":
query = query.orderby(order_by_column, order=frappe.query_builder.Order.desc)
else:
if order_by == "amc_expiry_date":
query = query.orderby(order_by_column.isnull(), order=frappe.query_builder.Order.desc)
query = query.orderby(order_by_column)
for key, value in filters.items():

View File

@@ -1121,17 +1121,20 @@ def insert_item_price(ctx: ItemDetailsCtx):
or getdate()
)
item_prices = frappe.get_all(
"Item Price",
filters={
"item_code": ctx.item_code,
"price_list": ctx.price_list,
"currency": ctx.currency,
"uom": ctx.stock_uom,
},
fields=["name", "price_list_rate", "valid_from", "valid_upto"],
order_by="valid_from desc, creation desc",
)
ip = frappe.qb.DocType("Item Price")
item_prices = (
frappe.qb.from_(ip)
.select(ip.name, ip.price_list_rate, ip.valid_from, ip.valid_upto)
.where(
(ip.item_code == ctx.item_code)
& (ip.price_list == ctx.price_list)
& (ip.currency == ctx.currency)
& (ip.uom == ctx.stock_uom)
)
.orderby(ip.valid_from.isnull(), order=frappe.qb.asc)
.orderby(ip.valid_from, order=frappe.qb.desc)
.orderby(ip.creation, order=frappe.qb.desc)
).run(as_dict=True)
item_price = next(
(
row
@@ -1233,6 +1236,7 @@ def get_item_price(
& (ip.price_list == pctx.price_list)
& (IfNull(ip.uom, "").isin(["", pctx.uom]))
)
.orderby(ip.valid_from.isnull(), order=frappe.qb.asc)
.orderby(ip.valid_from, order=frappe.qb.desc)
.orderby(IfNull(ip.batch_no, ""), order=frappe.qb.desc)
.orderby(ip.uom, order=frappe.qb.desc)