MagicStack / MagicStack/asyncpg

Unable to use f-string in SqlAlchemy ORM

Open
#781 1 comment 2 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**: 0.23.0
* **PostgreSQL version**: 12
* **Python version**: 3.9.4
* **Platform**: OSX
* **Do you use pgbouncer?**: No
* **Did you install asyncpg with pip?**: pipenv
* **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)?**: N/A

I'm trying to build a SQL statement with LIKE expression and a pattern but I'm getting an error.

Here is what I had in the code originally:
```
statement = select(Customer).options(sqlalchemy.orm.joinedload(Customer.transactions)).where(Customer.l_name.ilike(name))
```
and this worked great.

But if I want to use this statement with a % pattern like this:

```
statement = f'select(Customer).options(sqlalchemy.orm.joinedload(Customer.transactions)).where(Customer.l_name.ilike("%{name}%"))'
```

I get this error:

```
sqlalchemy.exc.ProgrammingError: (sqlalchemy.dialects.postgresql.asyncpg.ProgrammingError) : syntax error at or near "("
[SQL: select(Customer).options(sqlalchemy.orm.joinedload(Customer.transactions)).where(Customer.l_name.ilike("%%turing%%"))]
```

It does substitute the variable with a parameter but for some reason it adds an extra pair of percent signs and crashes.
How can I solve this problem?

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

No asyncpg file, test, or entry point is named. Start with the supplied SQLAlchemy expression and generated SQL, then verify whether the behavior reproduces in asyncpg or comes from constructing the ORM statement as a string; confirm the result against the listed PostgreSQL and asyncpg versions.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
22/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.