Skip to content

Commit 25a80f6

Browse files
committed
fix(#64): cartesian product in reln reflection to non-versioned classes
In many-to-many relationships when one of the classes is not versioned, we were not including the secondaryjoin in the filter criteria leading to a cartesian product.
1 parent b6d50b0 commit 25a80f6

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

sqlalchemy_history/relationship_builder.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ def criteria(self, obj):
8383
return self.many_to_one_criteria(obj)
8484
else:
8585
reflector = VersionExpressionReflector(obj, self.property)
86-
return reflector(self.property.primaryjoin)
86+
criteria = reflector(self.property.primaryjoin)
87+
88+
# For many-to-many relationships, we also need to include the secondary join
89+
if direction.name == "MANYTOMANY" and self.property.secondaryjoin is not None:
90+
criteria = sa.and_(criteria, reflector(self.property.secondaryjoin))
91+
return criteria
8792

8893
def many_to_many_criteria(self, obj):
8994
"""Returns the many-to-many query.

0 commit comments

Comments
 (0)