Skip to content

ORM from_statement().params() values lost when statement is compiled without cache key #13709

Closed

Description

@zzzeek

Originally reported in #13708. Regression from the new ExecutableStatement.params() implementation added in #7066 (see also #12915).

In 2.1, values passed to .params() on an ORM select().from_statement() construct are lost when the statement is compiled without a cache key, e.g. when the compiled cache is disabled via query_cache_size=0 / compiled_cache=None, or when the statement is stringified directly. This worked in 2.0.

import sqlalchemy as sa
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, Session


class Base(DeclarativeBase):
    pass


class Task(Base):
    __tablename__ = "task"
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str]


e = sa.create_engine("sqlite://", query_cache_size=0)
Base.metadata.create_all(e)

with Session(e) as s:
    s.add(Task(id=1, name="a"))
    s.commit()

    stmt = (
        sa.select(Task)
        .from_statement(sa.text("select * from task where name = :n"))
        .params(n="a")
    )

    print(stmt.compile().params)  # {'n': None}, expected {'n': 'a'}

    # raises: A value is required for bind parameter 'n'
    s.execute(stmt).scalars().all()

With the default compiled cache enabled the statement works, since the parameter values are carried in the cache key.

.params() on select(), text(), text().columns() and compound selects all work in this scenario; FromStatement is the only construct that does not register its own .params() with the compiler.

Workarounds: apply .params() to the text() construct itself, or pass the parameters to Session.execute().

Activity

  1. added this to the 2.1.x milestone on Oct 21, 2026
  2. added
    bug
    regression
    on Oct 21, 2026
  3. sqla-tester commented on Oct 21, 2026

    @sqla-tester
    Collaborator

    Mike Bayer has proposed a fix for this issue in the main branch:

    apply FromStatement params() when compiling without cache key https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/7098

  4. added a commit that references this issue on Oct 21, 2026
    8301bec
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

    bugormregression

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions