GithubHelp home page GithubHelp logo

Comments (3)

tomchristie avatar tomchristie commented on August 15, 2024

It'd be nice if the package could interpret the query passed in, and translate it.

I'm not sure exactly what you mean "interpret the query passed in, and translate it."

Happy to accept a pull request with a failing test for this and the proposed fix.

from databases.

szelenka avatar szelenka commented on August 15, 2024

I did some more digging, and it seems the problem is with using SQLAlchemy's bindparam in a statement and passing the values to the values param. I get similar errors with using sa.select with a bindparam in it.

_build_query is what throws the exception, but I couldn't find where a ClauseElement has a method "values" on it.

I tried converting the query to a str then wrapping it in text to assign the bindparams

    @staticmethod
    def _build_query(
        query: typing.Union[ClauseElement, str], values: dict = None
    ) -> ClauseElement:
        if isinstance(query, str):
            query = text(query)

            return query.bindparams(**values) if values is not None else query
        elif values:
            try:
                return query.values(**values)
            except AttributeError as ex:
                return text(str(query)).bindparams(**values)

        return query

However, this introduced a different problem inside _compile method when attempting to use the fetch_one/fetch_all methods, where the compiled._result_columns would contain an empty list:
https://github.com/encode/databases/blob/master/databases/backends/postgres.py#L179

The data is still returned correctly, and you can access them via named keys, but the shorthand for dict() conversion of the returned asyncpg.Record object fails due to compiled._result_columns being empty when this package attempts to create an instance of it here (where it passes in an empty result_columns):
https://github.com/encode/databases/blob/master/databases/backends/postgres.py#L144

A workaround is to simply not use bindparam and let SQLAlchemy handle all the compiling without passing values into the execute/fetch methods of this package.

I'll keep poking around to see if I can find a better solution, but may just end up stop using bindparams in my statements.

from databases.

tomchristie avatar tomchristie commented on August 15, 2024

I think we've several issues with .bindparam - https://github.com/encode/databases/search?q=bindparam&type=Issues (Specifically #79, #103)

Is there an easy way to detect if bindparam has been used? Perhaps we ought to just raise a hard error in that case, and treat any bindparam usage as currently being out of scope?

from databases.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.