Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 4791b75

Browse files
Return None from fetch_one if no results match
1 parent 56e2605 commit 4791b75

3 files changed

Lines changed: 6 additions & 0 deletions

File tree

databases/backends/mysql.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[typing.Mappin
105105
try:
106106
await cursor.execute(query, args)
107107
row = await cursor.fetchone()
108+
if row is None:
109+
return None
108110
metadata = ResultMetaData(context, cursor.description)
109111
return RowProxy(metadata, row, metadata._processors, metadata._keymap)
110112
finally:

databases/backends/postgres.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[typing.Mappin
121121
assert self._connection is not None, "Connection is not acquired"
122122
query, args, result_columns = self._compile(query)
123123
row = await self._connection.fetchrow(query, *args)
124+
if row is None:
125+
return None
124126
return Record(row, result_columns, self._dialect)
125127

126128
async def execute(self, query: ClauseElement, values: dict = None) -> typing.Any:

databases/backends/sqlite.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[typing.Mappin
9797

9898
async with self._connection.execute(query, args) as cursor:
9999
row = await cursor.fetchone()
100+
if row is None:
101+
return None
100102
metadata = ResultMetaData(context, cursor.description)
101103
return RowProxy(metadata, row, metadata._processors, metadata._keymap)
102104

0 commit comments

Comments
 (0)