make sure you are always using order_by if you're doing any kind if limit/offset (including if you call the first() accessor), see the notes at http://docs.sqlalchemy.org/en/rel_1_0/orm/loading_relationships.html#the-importance-of-ordering and http://docs.sqlalchemy.org/en/rel_1_0/faq/ormconfiguration.html#faq-subqueryload-limit-sort.

On 05/12/2016 08:30 PM, Marco wrote:
Greetings,

[SQLAlchemy 1.0.12]
We have observed situations in which the first in a list of objects
returned by a query with options(subqueryload(Class.relationship)) or
with lazy=subquery will have a missing relationship.

This is some pseudocode

[...]
class Parent(Base):
       id = Column(Integer, primary_key=True)

class Child(Base):
       id = Column(Integer, primary_key=True)
       parent_id = Column(Integer, ForeignKey(Parent.id))

Child.parent = relationship(Parent, lazy='subquery')

[...]
children = session.query(Child).all()

children[0].parent_id not None
children[0].parent None!!!

children[1].parent_id not None
children[1].parent not None

If I change the strategy to eager or lazy='select' things are fine.

I have not successfully isolated this issue into a toy model, but I'd be
happy to dig deeper if pointed to the relevant code. I can reproduce
this in our code easily.
Thanks!
Marco

--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to