Skip to content

Commit e7b400a

Browse files
authored
Merge pull request #206 from fastapi-startkit/task/where-lambda-subgroup-1397
fix(orm): grouped where(lambda) prefixes subgroup columns with the table (GH #205)
2 parents 3319171 + f497fa7 commit e7b400a

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

fastapi_startkit/src/fastapi_startkit/masoniteorm/models/builder.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,11 @@ async def chunk_by_id_desc(self, count: int, column: str = None, alias: str = No
466466
yield results
467467

468468
def new(self):
469-
return self.connection.query()
469+
# Carry the current table so a nested builder (e.g. a where(lambda ...)
470+
# subgroup) prefixes its columns correctly instead of rendering a
471+
# table-less ."column". Callers that want a different table override it
472+
# with .table(...) as usual.
473+
return self.connection.query().table(self._table)
470474

471475
def invalid_operator(self, operator):
472476
"""Determine whether an operator is not supported by the builder."""

fastapi_startkit/tests/masoniteorm/sqlite/builder/test_sqlite_query_builder.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,30 @@ async def test_delete_with_multiple_wheres(self):
134134
sql, bindings = mock_delete.call_args[0]
135135
self.assertEqual(sql, 'DELETE FROM "users" WHERE "age" = ? AND "profile" = ?')
136136
self.assertEqual(list(bindings), [20, 1])
137+
138+
async def test_where_grouped_lambda_prefixes_subgroup_columns(self):
139+
sql = (
140+
User.query()
141+
.where("name", "Joe")
142+
.where(lambda q: q.where("active", 1).where("age", ">", 20).or_where("id", ">=", 42))
143+
.to_sql()
144+
)
145+
self.assertEqual(
146+
sql,
147+
'SELECT * FROM "users" WHERE "users"."name" = \'Joe\' AND '
148+
'("users"."active" = \'1\' AND "users"."age" > \'20\' OR "users"."id" >= \'42\')',
149+
)
150+
151+
async def test_where_grouped_lambda_to_qmark_binding_order(self):
152+
builder = (
153+
User.query()
154+
.where("name", "Joe")
155+
.where(lambda q: q.where("active", 1).where("age", ">", 20).or_where("id", ">=", 42))
156+
)
157+
sql = builder.to_qmark()
158+
self.assertEqual(
159+
sql,
160+
'SELECT * FROM "users" WHERE "users"."name" = ? AND '
161+
'("users"."active" = ? AND "users"."age" > ? OR "users"."id" >= ?)',
162+
)
163+
self.assertEqual(list(builder._bindings), ["Joe", 1, 20, 42])

fastapi_startkit/uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)