Skip to content

Commit 62122c7

Browse files
authored
Merge pull request frappe#36194 from AarDG10/fix-null-fallback-pg
fix(query): fix ifnull fallback in postgres
2 parents 690c736 + 5c31a7d commit 62122c7

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

frappe/database/query.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,24 @@ def _get_ifnull_fallback(self, doctype: str, fieldname: str) -> str:
16071607
meta = frappe.get_meta(doctype)
16081608
df = meta.get_field(fieldname)
16091609
except Exception:
1610+
if frappe.db.db_type == "postgres":
1611+
"""check type and accordingly choose fallback (to avoid postgres type cast errors)"""
1612+
target_table = frappe.utils.get_table_name(doctype)
1613+
info_schema = frappe.qb.Schema("information_schema")
1614+
columns = info_schema.columns
1615+
current_schema = frappe.conf.get("db_schema", "public")
1616+
res = (
1617+
frappe.qb.from_(columns)
1618+
.select(columns.data_type)
1619+
.where(
1620+
(columns.table_name == target_table)
1621+
& (columns.column_name == fieldname)
1622+
& (columns.table_schema == current_schema)
1623+
)
1624+
).run(pluck=True)
1625+
data_type = res[0] if res else None
1626+
if data_type in ("smallint", "bigint", "int", "numeric"): # can add as needed
1627+
return "0"
16101628
return "''"
16111629

16121630
if df is None:

frappe/tests/test_query.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,6 +2289,15 @@ def test_permission_query_conditions_with_filter(self):
22892289
# the filter should still apply and return no results
22902290
self.assertEqual(len(result), 0, "Filter should not be bypassed by shared doc OR condition")
22912291

2292+
@run_only_if(db_type_is.POSTGRES)
2293+
def test_ifnull_fallback_postgres(self):
2294+
"""Test ifnull fallback in postgres"""
2295+
from frappe.database.query import Engine
2296+
2297+
engine = Engine()
2298+
self.assertEqual(engine._get_ifnull_fallback("Patch Log", "skipped"), "0")
2299+
self.assertEqual(engine._get_ifnull_fallback("Patch Log", "patch"), "''")
2300+
22922301

22932302
# This function is used as a permission query condition hook
22942303
def test_permission_hook_condition(user):

0 commit comments

Comments
 (0)