MagicStack / asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio.
Apache License 2.0
6.88k stars 398 forks source link

Impossible to select arbitrary column when using SQLAlchemy #681

Open agateblue opened 3 years ago

agateblue commented 3 years ago

I'm trying to use asyncpg with SQLAlchemy on a project of mine. However, the following constructs to select a subset of columns in the underlying table doesn't work:

query = mytable.select(mytable.c.date)
results = await database.fetch_all(query)

This raises the following error:

  File "./main.py", line 140, in post_telemetry
    tel_results = await vmo.database.fetch_all(tel_query)
  File "/usr/local/lib/python3.6/site-packages/databases/core.py", line 140, in fetch_all
    return await connection.fetch_all(query, values)
  File "/usr/local/lib/python3.6/site-packages/databases/core.py", line 239, in fetch_all
    return await self._connection.fetch_all(built_query)
  File "/usr/local/lib/python3.6/site-packages/databases/backends/postgres.py", line 160, in fetch_all
    rows = await self._connection.fetch(query, *args)
  File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 443, in fetch
    return await self._execute(query, args, 0, timeout)
  File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 1446, in _execute
    query, args, limit, timeout, return_status=return_status)
  File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 1454, in __execute
    return await self._do_execute(query, executor, timeout)
  File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 1466, in _do_execute
    stmt = await self._get_statement(query, None)
  File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 351, in _get_statement
    statement = await self._protocol.prepare(stmt_name, query, timeout)
  File "asyncpg/protocol/protocol.pyx", line 163, in prepare
asyncpg.exceptions.DatatypeMismatchError: argument of AND must be type boolean, not type timestamp with time zone

As far as I can't tell, asyncpg doesn't seem to support this feature that is present in SQLAlchemy since version 1.4, but I may be wrong.

Let me know if you need additional information.

agateblue commented 3 years ago

I may have found a workaround with query = sqlalchemy.sql.select(columns=[mytable.c.table])

elprans commented 3 years ago

This seems like a SQLAlchemy bug, please file a report there.