encode / databases

Async database support for Python. đź—„
https://www.encode.io/databases/
BSD 3-Clause "New" or "Revised" License
3.84k stars 261 forks source link

"asyncpg.exceptions.PostgresSyntaxError" While using sqlalchemy is_(None) method in query #221

Open balukrishnans opened 4 years ago

balukrishnans commented 4 years ago

Hai friends,

I am using sqlalchemy core + databases for building and executing query in my project.\ My steps to build the query is like

from sqlalchemy import *
from databases import Database

url = 'postgresql://username:password@localhost:5432/db_name'
meta = MetaData()
eng = create_engine(url, echo=True)
database = Database(url)

my_user_table = Table("my_user_table", meta, Column("name", String), Column("age", Integer))
meta.create_all(eng)

async def run():
    await database.connect()

    query = my_user_table.update().values({"name": bindparam("name_value")}).where(my_user_table.c.age.is_(bindparam("null_value")))
    values = {"name_value": "foo", "null_value": None}

    text_query = text(str(query))
    final_query = text_query.bindparams(**values)
    result = await database.fetch_all(query=final_query)

    await database.disconnect()
    print(result)

import asyncio
asyncio.run(run())

But this throws an error

  File "/Users/env_file/lib/python3.8/site-packages/databases/core.py", line 218, in fetch_all
    return await self._connection.fetch_all(self._build_query(query, values))
  File "/Users/env_file/lib/python3.8/site-packages/databases/backends/postgres.py", line 144, in fetch_all
    rows = await self._connection.fetch(query, *args)
  File "/Users/env_file/lib/python3.8/site-packages/asyncpg/connection.py", line 420, in fetch
    return await self._execute(query, args, 0, timeout)
  File "/Users/env_file/lib/python3.8/site-packages/asyncpg/connection.py", line 1402, in _execute
    result, _ = await self.__execute(
  File "/Users/env_file/lib/python3.8/site-packages/asyncpg/connection.py", line 1411, in __execute
    return await self._do_execute(query, executor, timeout)
  File "/Users/env_file/lib/python3.8/site-packages/asyncpg/connection.py", line 1423, in _do_execute
    stmt = await self._get_statement(query, None)
  File "/Users/env_file/lib/python3.8/site-packages/asyncpg/connection.py", line 328, in _get_statement
    statement = await self._protocol.prepare(stmt_name, query, timeout)
  File "asyncpg/protocol/protocol.pyx", line 163, in prepare
asyncpg.exceptions.PostgresSyntaxError: syntax error at or near "$2"

Nb: It's not because of the value None, I tried the query by replacing other values but the same error.

Prince2347X commented 3 years ago

did u got a fix? I'm facing the same error with postgres