For several years, I have been using a pattern for making a many-to-one 
relationship from *cls* to *remoteCls* with a one-to-many backref with a 
join condition cls.foreignKey == remoteCls.id, where
*cls* has a deletion flag _del which should exclude *cls* instances with del 
!= 0 from the backref collection.

Since the condition involving _del is only relevant in the one-to-many 
direction, I defined separate primaryjoin conditions which included this 
condition only for the backref.

br = backref(
    backref,
    collection_class=list,
    primaryjoin=and_(remoteCls.id==remote(getattr(cls, foreignKey)), 
cls._del==0))
    
rel = relationship(
    remoteCls,
    remote_side=remoteCls.id,
    primaryjoin=getattr(cls, foreignKey)==remoteCls.id,
    backref=br)
    
I have used this pattern successfully for years until I recently upgraded 
SqlAlchemy to the latest version and found that the join condition on the 
backref seems to be ignored and queries include instances that are flagged 
as deleted via the _del column. I tested several intermediate SqlAlchemy 
version and found that the first one which breaks the pattern is 0.9.4.

Subsequently I found that removing the primary join condition on the 
backref and including the _del != 0 condition in the forward primary join 
condition seems to restore the intended behavior, but now many queries 
involving large collections are dramatically slowed to make this solution 
unworkable.

I reviewed the desciptions of changes, but they did not clarify for me why 
the pattern above does not work any more. Is there a flaw in my code that I 
am missing?

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to