You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In psycopg2, register_adapter along with utility functions provides a useful way to map custom Python classes into valid representations, often by relatively simple behaviors such as casting to str. There is no equivalent in asyncpg meaning moving from psycopg2 to asyncpg requires adding lots of str() casts to add and select queries.
The closest equivalent is set_type_codec but this only works for types that are registered with Postgres, not custom Python classes.
The text was updated successfully, but these errors were encountered:
dgilman-hrp
changed the title
Add equivalent to sycopg2 register_adapter
Add equivalent to psycopg2 register_adapterOct 4, 2024
asyncpg normally relies on duck typing when accepting inputs, e.g if an argument is an integer, asyncpg will happily accept an object that implements __int__(), which is in line with Python's implicit casting tradition. Are you looking for a way to stringify objects specifically?
What I have is a class which inherits from Pydantic ConstrainedStr. When I use it in a query, I get an error Expected unicode, unless I explicitly cast it to str before passing it to the query.
Adding __str__ to the custom class does not solve the problem.
In psycopg2,
register_adapter
along with utility functions provides a useful way to map custom Python classes into valid representations, often by relatively simple behaviors such as casting tostr
. There is no equivalent in asyncpg meaning moving from psycopg2 to asyncpg requires adding lots ofstr()
casts to add and select queries.The closest equivalent is
set_type_codec
but this only works for types that are registered with Postgres, not custom Python classes.The text was updated successfully, but these errors were encountered: