From d082017d57a0631c206aad2192b487486a5e2846 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 29 Oct 2020 00:29:41 +0100 Subject: [PATCH] Use unknown oid passing NULL to queries Passing text makes for a cast rejected by fields with a stricter cast. --- psycopg3/psycopg3/utils/queries.py | 3 ++- tests/test_adapt.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/psycopg3/psycopg3/utils/queries.py b/psycopg3/psycopg3/utils/queries.py index 43ded029e..01dc771f5 100644 --- a/psycopg3/psycopg3/utils/queries.py +++ b/psycopg3/psycopg3/utils/queries.py @@ -18,6 +18,7 @@ if TYPE_CHECKING: from ..proto import Transformer TEXT_OID = 25 # TODO: builtins["text"].oid +UNKNOWN_OID = 705 # TODO: builtins["unknown"].oid class QueryPart(NamedTuple): @@ -88,7 +89,7 @@ class PostgresQuery: self.types.append(dumper.oid) else: self.params.append(None) - self.types.append(TEXT_OID) + self.types.append(UNKNOWN_OID) else: for i in range(len(params)): param = params[i] diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 893b25642..8399d42ef 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -130,6 +130,23 @@ def test_load_cursor_ctx_nested(conn, sql, obj, fmt_out): assert res == obj +@pytest.mark.parametrize("fmt_in", [Format.TEXT, Format.BINARY]) +def test_none_type_argument(conn, fmt_in): + cur = conn.cursor() + cur.execute( + """ + create table test_none_type_argument ( + id serial primary key, num integer + ) + """ + ) + cur.execute( + "insert into test_none_type_argument (num) values (%s) returning id", + (None,), + ) + assert cur.fetchone()[0] + + def make_dumper(suffix): """Create a test dumper appending a suffix to the bytes representation.""" -- 2.47.2