[Sqlalchemy-tickets] Issue #3387: add_entity and before_compile event (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
|
From: jaredks <iss...@bi...> - 2015-04-25 22:23:13
|
New issue 3387: add_entity and before_compile event https://bitbucket.org/zzzeek/sqlalchemy/issue/3387/add_entity-and-before_compile-event jaredks: When adding an entity to a query "before_compile" the query is correctly executed but the row processor(s) used in `orm.loading.instances` still refer to the original query object - specifically the `_entities` list. So if you had say related `User` and `Address` models and some query, ``` #!python q = sess.query(User).filter_by(first_name='John') ``` and a listener, ``` #!python @event.listens_for(SomeQuery, 'before_compile', retval=True) def change_query(query): return query.add_entity(Address).join(Address) ``` You would expect the query to yield `KeyedTuple` but instead `User` entity is used. A potential solution would be to change `Query._execute_and_instances` to return, ``` #!python loading.instances(querycontext.query, result, query context) ``` instead of, ``` #!python loading.instances(self, result, querycontext) ``` But this change may have other side-effects? |