aiopg icon indicating copy to clipboard operation
aiopg copied to clipboard

Expanding bindparam doesn't work

Open decaz opened this issue 5 years ago • 2 comments

The bindparam(expanding=True) is intended for binding the sequence which is being used within IN expression: https://docs.sqlalchemy.org/en/13/core/sqlelement.html#sqlalchemy.sql.expression.bindparam.params.expanding.

SQLAlchemy (working):

from sqlalchemy import Column, Integer, bindparam, create_engine, select
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)

engine = create_engine('postgresql://postgres@localhost/postgres', echo=True)
Base.metadata.create_all(engine)

bind = bindparam('ids', expanding=True)
stmt = select([User]).where(User.id.in_(bind))
engine.execute(stmt, ids=[1, 2, 3])

aiopg.sa (NOT working):

import asyncio

import sqlalchemy as sa
from aiopg.sa import create_engine

metadata = sa.MetaData()
users = sa.Table('users', metadata, sa.Column('id', sa.Integer, primary_key=True))

bind = sa.bindparam('ids', expanding=True)

async def main():
    async with create_engine('postgresql://postgres@localhost/postgres') as engine:
        async with engine.acquire() as conn:
            await conn.execute('CREATE TABLE IF NOT EXISTS users (id serial PRIMARY KEY)')
            await conn.execute(users.select().where(users.c.id.in_(bind)), ids=[1, 2, 3])

asyncio.run(main())

Here is the traceback:

Traceback (most recent call last):
  File "test.py", line 19, in <module>
    asyncio.run(main())
  File "/home/decaz/.pyenv/versions/3.8.1/lib/python3.8/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/home/decaz/.pyenv/versions/3.8.1/lib/python3.8/asyncio/base_events.py", line 612, in run_until_complete
    return future.result()
  File "test.py", line 16, in main
    await conn.execute(users.select().where(users.c.id.in_(bind)), ids=[1, 2, 3])
  File "/home/decaz/workspace/test/.venv/lib/python3.8/site-packages/aiopg/sa/connection.py", line 120, in _execute
    await cursor.execute(str(compiled), post_processed_params[0])
  File "/home/decaz/workspace/test/.venv/lib/python3.8/site-packages/aiopg/cursor.py", line 113, in execute
    await self._conn._poll(waiter, timeout)
  File "/home/decaz/workspace/test/.venv/lib/python3.8/site-packages/aiopg/connection.py", line 207, in _poll
    await asyncio.wait_for(self._waiter, timeout, loop=self._loop)
  File "/home/decaz/.pyenv/versions/3.8.1/lib/python3.8/asyncio/tasks.py", line 483, in wait_for
    return fut.result()
  File "/home/decaz/workspace/test/.venv/lib/python3.8/site-packages/aiopg/connection.py", line 106, in _ready
    state = self._conn.poll()
psycopg2.errors.SyntaxError: syntax error at or near "["
LINE 3: WHERE users.id IN ([EXPANDING_ids])

decaz avatar Feb 06 '20 16:02 decaz

i have same error, heeeelp (

Clarxxon avatar Jun 01 '21 13:06 Clarxxon

Three years later, I got the same error when using Gino (1.1.0rc1). Any idea/solution to avoid it?

mgoeminne avatar Mar 24 '23 07:03 mgoeminne