diff --git a/asyncpg/utils.py b/asyncpg/utils.py index 3940e04d..5c1ca699 100644 --- a/asyncpg/utils.py +++ b/asyncpg/utils.py @@ -42,4 +42,11 @@ async def _mogrify(conn, query, args): # Finally, replace $n references with text values. return re.sub( - r'\$(\d+)\b', lambda m: textified[int(m.group(1)) - 1], query) + r"\$(\d+)\b", + lambda m: ( + textified[int(m.group(1)) - 1] + if textified[int(m.group(1)) - 1] is not None + else "NULL" + ), + query, + ) diff --git a/tests/test_copy.py b/tests/test_copy.py index be2aabaf..e119e6d8 100644 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -148,12 +148,14 @@ async def test_copy_from_query_with_args(self): res = await self.con.copy_from_query(''' SELECT - i, i * 10 + i, + i * 10, + $2::text FROM generate_series(1, 5) AS i WHERE i = $1 - ''', 3, output=f) + ''', 3, None, output=f) self.assertEqual(res, 'COPY 1') @@ -161,7 +163,7 @@ async def test_copy_from_query_with_args(self): self.assertEqual( output, [ - '3\t30', + '3\t30\t\\N', '' ] )