-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Migrated issue, originally created by Torsten Landschoff (@bluehorn)
Sorry for the bad title, I have no idea how to summarize this. I tried to upgrade the SQLAlchemy version we are using (still at 0.9.10 because of the change in behaviour that setting a mapped relation overrides the column value that I once reported as a bug).
Upgrading to 1.0 creates unexpected failures where our code tries to load a mapped entity together with a related entity by a join. Basically the query
session.query(A, B).join("b")
stopped working.
This seems to affect both 1.0 and 1.1 branches. Full example code attached as joinfail.py.
For SQLAlchemy 0.9.10 this works as expected:
(python27)torsten.landschoff@horatio:~$ pip install SQLAlchemy==0.9.10
[...]
Successfully installed SQLAlchemy-0.9.10
(python27)torsten.landschoff@horatio:~$ python joinfail.py
SELECT people.type AS people_type, engineers.id AS engineers_id, people.id AS people_id, engineers.primary_language AS engineers_primary_language, engineers.manager_id AS engineers_manager_id, people_1.type AS people_1_type, managers_1.id AS managers_1_id, people_1.id AS people_1_id, seen_1.id AS seen_1_id, seen_1.timestamp AS seen_1_timestamp, seen_2.id AS seen_2_id, seen_2.timestamp AS seen_2_timestamp
FROM people JOIN engineers ON people.id = engineers.id JOIN (people AS people_1 JOIN managers AS managers_1 ON people_1.id = managers_1.id) ON managers_1.id = engineers.manager_id LEFT OUTER JOIN seen AS seen_1 ON people.id = seen_1.id LEFT OUTER JOIN seen AS seen_2 ON people_1.id = seen_2.id
Running the same example against SQLAlchemy 1.0.16 results in this:
(python27)torsten.landschoff@horatio:~$ pip install SQLAlchemy==1.0.16
[...]
Successfully installed SQLAlchemy-1.0.16
(python27)torsten.landschoff@horatio:~$ python joinfail.py
Traceback (most recent call last):
File "joinfail.py", line 50, in <module>
print session.query(Engineer, Manager).join("manager")
File "/opt/scalesdk/python27/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 3417, in __str__
return str(self._compile_context().statement)
File "/opt/scalesdk/python27/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 3261, in _compile_context
strategy(*rec[1:])
File "/opt/scalesdk/python27/lib/python2.7/site-packages/sqlalchemy/orm/strategies.py", line 1351, in _create_eager_join
if parentmapper.isa(self.parent) else self.parent)
AttributeError: 'NoneType' object has no attribute 'isa'
The exact same behaviour is still present in 1.1.4:
(python27)torsten.landschoff@horatio:~$ pip install SQLAlchemy==1.1.4
[...]
Successfully installed SQLAlchemy-1.1.4
(python27)torsten.landschoff@horatio:~$ python joinfail.py
Traceback (most recent call last):
File "joinfail.py", line 50, in <module>
print session.query(Engineer, Manager).join("manager")
File "/opt/scalesdk/python27/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2800, in __str__
context = self._compile_context()
File "/opt/scalesdk/python27/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 3312, in _compile_context
strategy(*rec[1:])
File "/opt/scalesdk/python27/lib/python2.7/site-packages/sqlalchemy/orm/strategies.py", line 1372, in _create_eager_join
if parentmapper.isa(self.parent) else self.parent)
AttributeError: 'NoneType' object has no attribute 'isa'
I also tried with the current master 2b4d028, still getting the same error.
Removing the ``lazy="joined`" option on the backref "last_seen" makes it working again, unfortunately we actually want to have that information loaded when available - at least on the toplevel entity. I am not sure how to disable it on the child only.
Still impressed how SQLAlchemy can handle stuff like this (it worked in 0.9.10).
Thanks!
Torsten
Attachments: joinfail.py