Skip to content

Query compilation #6096

Closed
Closed

Description

@noisywiz

Describe the bug
My query:

query = select([Department], limit=limit, offset=offset).where(
    Department.department_id.in_([x.department_id for x in permitted_departments]))

turns into:

SELECT department.department_id, department.department_name 
FROM department 
WHERE department.department_id IN ([POSTCOMPILE_department_id_1]) 

with error:

ERROR: syntax error at or near "["

Expected behavior

SELECT department.department_id, department.department_name 
FROM department 
WHERE department.department_id IN (%(department_id_1)s)

Versions.

  • OS: Debian buster
  • Python: 3.8
  • SQLAlchemy: 1.4.1
  • Database: Postgres

Activity

  1. CaselIT commented on Mar 19, 2021

    @CaselIT
    Member

    Hi,
    How are you using the query? What you have posted seems like str(query). That will not give you valid sql in the general case.

  2. added
    awaiting infowaiting for the submitter to give more information
    questionissue where a "fix" on the SQLAlchemy side is unlikely, hence more of a usage question
    and removed
    requires triageNew issue that requires categorization
    on Mar 19, 2021
  3. noisywiz commented on Mar 19, 2021

    @noisywiz
    Author

    Hi,
    How are you using the query? What you have posted seems like str(query). That will not give you valid sql in the general case.

    Hi, it's debug print query.compile(dialect=postgresql.dialect())). Actualy I fetch this with databases.Database.fetch_all method (https://pypi.org/project/databases/) and I'm fine with SQLAlchemy==1.3.23

  4. removed
    awaiting infowaiting for the submitter to give more information
    on Mar 19, 2021
  5. CaselIT commented on Mar 19, 2021

    @CaselIT
    Member

    Hi,

    In 1.4 how the query is complied has changed to enable the caching of the generated strings.
    To enable compilation if the query in a more complete way, you can use: compile(dialect=dialect(), compile_kwargs={"literal_binds"=True, "render_postcompile"=True})

    Note that non all datatypes support literal compile with "literal_binds"=True, so it may error on more complex types

  6. CaselIT commented on Mar 20, 2021

    @CaselIT
    Member

    @noisywiz do you have another more specific question or can we move this to a discussion?

  7. added
    awaiting infowaiting for the submitter to give more information
    on Mar 20, 2021
  8. noisywiz commented on Mar 20, 2021

    @noisywiz
    Author

    @noisywiz do you have another more specific question or can we move this to a discussion?

    Yes close it. Thanks for help!

  9. locked and limited conversation to collaborators on Mar 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting infowaiting for the submitter to give more informationquestionissue where a "fix" on the SQLAlchemy side is unlikely, hence more of a usage question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions