Support getting the engine from column expressions against scalar subqueries

Issue #1326 resolved
Former user created an issue

(original reporter: ged) The attached test case fails with the following traceback:

Traceback (most recent call last):
  File "test_getenginefromfunc.py", line 61, in <module>
    print session.query(func.sum(User.score)).scalar()
  File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/query.py", line 1268, in scalar
    ret = list(self)[0](0)
  File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/query.py", line 1280, in __iter__
    return self._execute_and_instances(context)
  File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/query.py", line 1283, in _execute_and_instances
    result = self.session.execute(querycontext.statement, params=self._params, mapper=self._mapper_zero_or_none())
  File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/session.py", line 752, in execute
    engine = self.get_bind(mapper, clause=clause, **kw)
  File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/session.py", line 890, in get_bind
    ', '.join(context)))
sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on SQL expression or this Session

Binding the session with the engine makes it work. IMHO, it shouldn't be needed since the tables that are selected from are within a metadata object which is already bound to it.

Comments (4)

  1. Mike Bayer repo owner

    Replying to ged:

    Binding the session with the engine makes it work. IMHO, it shouldn't be needed since the tables that are selected from are within a metadata object which is already bound to it.

    the key phrase here is "tables that are selected from". The statement has no FROM clause. The scalar subquery in its column expression does, but most constructs look to the immediate FROM clauses as the source of binds.

  2. Mike Bayer repo owner
    • The primary :class:.Mapper of a :class:.Query is now passed to the :meth:.Session.get_bind method when calling upon :meth:.Query.count, :meth:.Query.update, :meth:.Query.delete, as well as queries against mapped columns, :obj:.column_property objects, and SQL functions and expressions derived from mapped columns. This allows sessions that rely upon either customized :meth:.Session.get_bind schemes or "bound" metadata to work in all relevant cases. fixes #3227 fixes #3242 fixes #1326

    → <<cset 611883ffb35c>>

  3. Log in to comment