Skip to content

fix(#64): cartesian product in reln reflection to non-versioned classes#154

Merged
winwinashwin merged 1 commit intomainfrom
ashwin/contrib
Dec 28, 2025
Merged

fix(#64): cartesian product in reln reflection to non-versioned classes#154
winwinashwin merged 1 commit intomainfrom
ashwin/contrib

Conversation

@winwinashwin
Copy link
Contributor

Fixes: #64

# For many-to-many relationships, we also need to include the secondary join
if direction.name == "MANYTOMANY" and self.property.secondaryjoin is not None:
criteria = sa.and_(criteria, reflector(self.property.secondaryjoin))
return criteria
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can have this unit test in tests/relationships/test_non_versioned_classes.py

def test_no_cartesian_product_with_multiple_unrelated_tags(self):
    # Create an article with one tag
    article = self.Article(name="Some article")
    tag1 = self.Tag(name="tag1")
    article.tags.append(tag1)
    self.session.add(article)
    self.session.commit()

    # Create another article with a different tag
    article2 = self.Article(name="Another article")
    tag2 = self.Tag(name="tag2")
    article2.tags.append(tag2)
    self.session.add(article2)
    self.session.commit()

    # Ensure the first article's version only has its own tag, not all tags
    assert len(article.versions[0].tags) == 1
    assert article.versions[0].tags[0] == tag1

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.
@winwinashwin winwinashwin merged commit ed27e55 into main Dec 28, 2025
12 checks passed
@winwinashwin winwinashwin deleted the ashwin/contrib branch December 28, 2025 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants