]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Use unknown oid passing NULL to queries
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 28 Oct 2020 23:29:41 +0000 (00:29 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 28 Oct 2020 23:29:41 +0000 (00:29 +0100)
Passing text makes for a cast rejected by fields with a stricter cast.

psycopg3/psycopg3/utils/queries.py
tests/test_adapt.py

index 43ded029e91b28ebdb0005d1d6c38f1aa6eb0b48..01dc771f582416601d874a614a5c93df0bd4f840 100644 (file)
@@ -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]
index 893b256424e50b2ff3c5b39b60921c15447ab9fb..8399d42ef7371ebdedf012be17df0bc1beab9fa6 100644 (file)
@@ -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."""