From: Daniele Varrazzo Date: Wed, 28 Oct 2020 23:29:41 +0000 (+0100) Subject: Use unknown oid passing NULL to queries X-Git-Tag: 3.0.dev0~419 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d082017d57a0631c206aad2192b487486a5e2846;p=thirdparty%2Fpsycopg.git Use unknown oid passing NULL to queries Passing text makes for a cast rejected by fields with a stricter cast. --- 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."""