Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 9e58341

Browse files
committed
Change the dialect for aiopg for better support.
1 parent 3052211 commit 9e58341

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

databases/backends/aiopg.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import getpass
2+
import json
23
import logging
34
import typing
45
import uuid
56

67
import aiopg
7-
from sqlalchemy.dialects.postgresql import pypostgresql
8+
from aiopg.sa.engine import APGCompiler_psycopg2
9+
from sqlalchemy.dialects.postgresql.psycopg2 import PGDialect_psycopg2
810
from sqlalchemy.engine.interfaces import Dialect, ExecutionContext
911
from sqlalchemy.engine.result import ResultMetaData, RowProxy
1012
from sqlalchemy.sql import ClauseElement
@@ -26,8 +28,10 @@ def __init__(
2628
self._pool = None
2729

2830
def _get_dialect(self) -> Dialect:
29-
dialect = pypostgresql.dialect(paramstyle="pyformat")
30-
31+
dialect = PGDialect_psycopg2(
32+
json_serializer=json.dumps, json_deserializer=lambda x: x
33+
)
34+
dialect.statement_compiler = APGCompiler_psycopg2
3135
dialect.implicit_returning = True
3236
dialect.supports_native_enum = True
3337
dialect.supports_smallserial = True # 9.2+

tests/test_databases.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import datetime
33
import decimal
44
import functools
5-
import json
65
import os
76

87
import pytest
@@ -514,14 +513,10 @@ async def test_json_field(database_url):
514513
async with Database(database_url) as database:
515514
async with database.transaction(force_rollback=True):
516515
# execute()
517-
query = session.insert()
518516
data = {"text": "hello", "boolean": True, "int": 1}
519517
values = {"data": data}
520-
521-
if database.url.scheme == "postgresql+aiopg":
522-
await database.execute(query, {"data": json.dumps(data)})
523-
else:
524-
await database.execute(query, values)
518+
query = session.insert()
519+
await database.execute(query, values)
525520

526521
# fetch_all()
527522
query = session.select()
@@ -544,6 +539,7 @@ async def test_custom_field(database_url):
544539
# execute()
545540
query = custom_date.insert()
546541
values = {"title": "Hello, world", "published": today}
542+
547543
await database.execute(query, values)
548544

549545
# fetch_all()

0 commit comments

Comments
 (0)