MagicStack / MagicStack/asyncpg

Impossible to select arbitrary column when using SQLAlchemy

Open
#681 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
8.1k
Forks
468
PR merge metrics
No merged PRs in 30d

Description

* **asyncpg version**: asyncpg==0.21.0
* **PostgreSQL version**: 13
* **Python version**: 3.6
* **Platform**: Alpine Linux (3.8)
* **Do you use pgbouncer?**: no
* **Did you install asyncpg with pip?**: yes
* **If you built asyncpg locally, which version of Cython did you use?**: N/A
* **Can the issue be reproduced under both asyncio and
[uvloop](https://github.com/magicstack/uvloop)?**: I don't know, I'm using uvloop right now

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:

```python
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](https://docs.sqlalchemy.org/en/14/core/tutorial.html#selecting-specific-columns), but I may be wrong.

Let me know if you need additional information.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the SQLAlchemy column-selection query with the reported Python, PostgreSQL, and package versions, then inspect the SQL handed to asyncpg through databases. Confirm whether the generated query causes the datatype-mismatch error; done means selecting a subset of columns executes successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python, sqlalchemy
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.