summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects/postgresql/asyncpg.py
diff options
context:
space:
mode:
authormike bayer <mike_mp@zzzcomputing.com>2021-11-03 14:50:36 +0000
committerGerrit Code Review <gerrit@ci3.zzzcomputing.com>2021-11-03 14:50:36 +0000
commit886e17d95607e3bee94ead789ca1fa4a30ee3f09 (patch)
tree1073f52d1449afda079dd57cb852a46d7f59cf44 /lib/sqlalchemy/dialects/postgresql/asyncpg.py
parent6d4009c0cb2e9ae224e3013e872f0e2ee95ae14e (diff)
parent96c294da8a50d692b3f0b8e508dbbca5d9c22f1b (diff)
downloadsqlalchemy-886e17d95607e3bee94ead789ca1fa4a30ee3f09.tar.gz
Merge "Gracefully degrade unsupported types with asyncpg" into main
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/asyncpg.py')
-rw-r--r--lib/sqlalchemy/dialects/postgresql/asyncpg.py43
1 files changed, 29 insertions, 14 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/asyncpg.py b/lib/sqlalchemy/dialects/postgresql/asyncpg.py
index 913b93159..548b7ecfb 100644
--- a/lib/sqlalchemy/dialects/postgresql/asyncpg.py
+++ b/lib/sqlalchemy/dialects/postgresql/asyncpg.py
@@ -878,6 +878,8 @@ class PGDialect_asyncpg(PGDialect):
use_native_uuid = True
+ _exclude_type_codecs = util.EMPTY_SET
+
colspecs = util.update_copy(
PGDialect.colspecs,
{
@@ -1031,21 +1033,34 @@ class PGDialect_asyncpg(PGDialect):
See https://github.com/MagicStack/asyncpg/issues/623 for reference
on why it's set up this way.
+
+ Also, see https://github.com/sqlalchemy/sqlalchemy/issues/7284 for
+ the rationale behind adding self._exclude_type_codecs
"""
- await conn._connection.set_type_codec(
- "json",
- encoder=str.encode,
- decoder=_json_decoder,
- schema="pg_catalog",
- format="binary",
- )
- await conn._connection.set_type_codec(
- "jsonb",
- encoder=_jsonb_encoder,
- decoder=_jsonb_decoder,
- schema="pg_catalog",
- format="binary",
- )
+
+ if "json" not in self._exclude_type_codecs:
+ try:
+ await conn._connection.set_type_codec(
+ "json",
+ encoder=str.encode,
+ decoder=_json_decoder,
+ schema="pg_catalog",
+ format="binary",
+ )
+ except ValueError:
+ self._exclude_type_codecs |= {"json"}
+
+ if "jsonb" not in self._exclude_type_codecs:
+ try:
+ await conn._connection.set_type_codec(
+ "jsonb",
+ encoder=_jsonb_encoder,
+ decoder=_jsonb_decoder,
+ schema="pg_catalog",
+ format="binary",
+ )
+ except ValueError:
+ self._exclude_type_codecs |= {"jsonb"}
def connect(conn):
conn.await_(_setup_type_codecs(conn))