Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.

Conversation

@whitdog47
Copy link
Contributor

In our current implementation, we encountered an error when attempting to join models within our query construction logic. The error message indicated:

Mapped instance expected for relationship comparison to object. Classes, queries and other SQL elements are not accepted in this context; for comparison with a subquery, use Case.assignee.has(**criteria).

This issue arose because our logic was attempting to track joined models using InstrumentedAttribute objects, which led to incorrect comparisons based on object identity rather than logical identity.

Changes Made:

  1. Tracking Models Directly:

    • Modified the logic to track the actual model or table objects instead of InstrumentedAttribute objects. This is done by using the parent attribute when available, and defaulting to the joined_model itself when parent is not present. This ensures that we are consistently tracking the logical entities being joined, regardless of whether they are accessed via ORM attributes or directly as tables.
  2. Avoiding Duplicate Joins:

    • Implemented a check to determine whether a model or table has already been joined. This is achieved by maintaining a list of joined models or tables (joined_models) and checking for membership before appending new joins. This prevents redundant joins and helps maintain query efficiency.

@whitdog47 whitdog47 requested review from Copilot and wssheldon May 22, 2025 17:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes issues with tracking joined models in query construction by unifying model identity and preventing redundant joins.

  • Tracks the actual model/table via parent or itself to ensure consistent identity.
  • Checks and skips already-joined entities to avoid duplicate joins.
Comments suppressed due to low confidence (2)

src/dispatch/database/service.py:489

  • There’s no test coverage for the new deduplication logic. Add tests that verify both standard and alias-based joins are correctly added or skipped.
query = query.join(joined_model, isouter=is_outer)

src/dispatch/database/service.py:487

  • [nitpick] The variable name model_or_table is generic. Consider renaming to join_target or entity_key to more clearly express its role in deduplication.
model_or_table = getattr(joined_model, "parent", joined_model)

@wssheldon wssheldon added the bug Something isn't working label May 22, 2025
@whitdog47 whitdog47 merged commit 7d8e1fb into main May 22, 2025
9 of 10 checks passed
@whitdog47 whitdog47 deleted the fix/database-joined-model-check branch May 22, 2025 20:19
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants