Skip to content

sqlalchemy.text() used for qmark queries in SQLite #57

Description

@marcelmindemann

Hello,
I am using sqlc v1.27.0, sqlc-gen-python 1.2.0 and sqlalchemy 1.4.54.

Example query:

-- name: GetUser :one
SELECT * FROM users
WHERE id = ?

Generated code:

GET_USER= """-- name: get_user \\:one
SELECT id FROM users
WHERE id = ?
"""

class Querier:
    def __init__(self, conn: sqlalchemy.engine.Connection):
        self._conn = conn

    def get_user(self, *, id: Any) -> Optional[models.User]:
        row = self._conn.execute(sqlalchemy.text(GET_USER), {"p1": id}).first()
        if row is None:
            return None
        return models.User(
            id=row[0],
        )

This code does not execute. Running

users.get_user(id=1)

gives error

ProgrammingError: (sqlite3.ProgrammingError) Incorrect number of bindings supplied. The current statement uses 1, and there are 0 supplied.

This happens because sqlalchemy.text() expects bound parameters to use where id = :id style. By using qmark style bound parameters (where id = ?), this query is not recognized to be a prepared statement and is probably run as-is, and SQLalchemy does not send parameter values with it. Reference: https://docs.sqlalchemy.org/en/14/core/tutorial.html#using-textual-sql

I cannot find a workaround for this. Using

-- name: GetUser :one
SELECT * FROM users
WHERE id = :id

or

-- name: GetUser :one
SELECT * FROM users
WHERE id = sqlc.arg(id)

yields

GET_USER= """-- name: get_tree \\:one
SELECT id, email, full_name FROM users
WHERE id = ?1

which causes the same error. If you want to use sqlalchemy.text(), you have to use :parameter syntax.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions