Skip to content

Fix query builder crashes for Projects relationship queries#8087

Open
acwhite211 wants to merge 4 commits into
mainfrom
issue-8083
Open

Fix query builder crashes for Projects relationship queries#8087
acwhite211 wants to merge 4 commits into
mainfrom
issue-8083

Conversation

@acwhite211
Copy link
Copy Markdown
Member

@acwhite211 acwhite211 commented May 15, 2026

Fixes #8083

Fixes back-end crashes when Query Builder paths traverse to Projects, including direct Collection Object queries and nested related-table paths. The stored query SQLAlchemy model layer now maps the CollectionObject.projects / Project.collectionObjects many-to-many relationship through project_colobj, and formatted Project relationship aggregation now correlates through that join table instead of assuming a direct foreign key.

Checklist

  • Self-review the PR after opening it to make sure the changes look good and
    self-explanatory (or properly documented)
  • Add relevant issue to release milestone
  • Add pr to documentation list
  • Add automated tests

Testing instructions

  • Go to Query Builder and select Collection Object as the base table.
  • Map a field from Projects, such as Projects -> Project Name, and run the query.
  • Map Projects -> formatted and run the query.
  • Select another base table that can reach Collection Objects, such as Accession or Locality.
  • Traverse from that base table to Collection Objects, then to Projects, and map a Project field.
  • Run the query and confirm it returns results or blanks without a backend error.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added full support for many-to-many relationships in queries and data aggregation.
  • Bug Fixes

    • Fixed resolution of many-to-many relationships that were previously unavailable in queries.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f882ea7c-dbc3-4fba-b5dc-2aa24c798ff9

📥 Commits

Reviewing files that changed from the base of the PR and between cc41ace and 784b01f.

📒 Files selected for processing (4)
  • specifyweb/backend/stored_queries/build_models.py
  • specifyweb/backend/stored_queries/format.py
  • specifyweb/backend/stored_queries/tests/test_execution/test_run_ephemeral_query.py
  • specifyweb/backend/stored_queries/tests/tests.py
💤 Files with no reviewable changes (1)
  • specifyweb/backend/stored_queries/tests/tests.py

📝 Walkthrough

Walkthrough

This PR adds end-to-end support for many-to-many relationships in the query builder, enabling queries through join tables like Project-CollectionObject that previously crashed the backend. It introduces join metadata types and helpers in build_models.py, integrates them into ORM setup and aggregation queries, and validates the feature with comprehensive tests.

Changes

Many-to-many relationship support

Layer / File(s) Summary
Many-to-many metadata types and helper functions
specifyweb/backend/stored_queries/build_models.py
Introduces ManyToManyJoinInfo NamedTuple with join table and column metadata, MANY_TO_MANY_TABLES configuration, and get_many_to_many_join_info()/make_many_to_many_table() helpers to compute and construct join table definitions.
ORM table and relationship setup
specifyweb/backend/stored_queries/build_models.py
Extends make_tables() to create and register configured join tables, and updates map_classes() to wire ManyToMany relationships via SQLAlchemy's secondary parameter with explicit primaryjoin/secondaryjoin conditions.
Aggregation query support for many-to-many
specifyweb/backend/stored_queries/format.py
Imports join metadata helpers and updates ObjectFormatter.aggregate() to conditionally build correlated subqueries joining through the secondary table using computed columns, with fallback to single-column logic.
Test coverage and expected behavior update
specifyweb/backend/stored_queries/tests/test_execution/test_run_ephemeral_query.py, specifyweb/backend/stored_queries/tests/tests.py
Adds PROJECT_FORMATTERS fixture and test_query_project_many_to_many_relationship() to validate ephemeral queries resolve Project relationships correctly across collection objects and accessions; removes expected error entries now that Project relationships are supported.
🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Testing Instructions ⚠️ Warning PR summary claims "Testing instructions provided" but no formal user-facing testing instructions are documented. Automated tests cover the changes, but manual QA steps for Query Builder are missing. Add explicit testing instructions to PR: 1) Query Builder with Collection Object + Projects fields, 2) Nested relationships (e.g., Accession → Projects), 3) Formatted Project aggregation. Code tests exist but lack user documentation.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the primary change: fixing query builder crashes when accessing the Projects relationship, which aligns with the main objective of adding many-to-many relationship support.
Linked Issues check ✅ Passed The pull request comprehensively addresses issue #8083 by implementing many-to-many relationship mapping for Projects, preventing backend crashes when querying Projects relationships from Collection Objects and other tables.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing Projects relationship queries: build_models.py adds many-to-many support, format.py updates aggregation logic, tests validate the fix, and test expectations are updated accordingly.
Automatic Tests ✅ Passed PR includes automatic tests for many-to-many Projects relationships. New test covers direct and related queries with proper assertions and mocking.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue-8083

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@acwhite211 acwhite211 marked this pull request as ready for review May 18, 2026 14:41
@acwhite211 acwhite211 requested review from a team May 18, 2026 14:41
Copy link
Copy Markdown
Collaborator

@emenslin emenslin left a comment

Choose a reason for hiding this comment

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

  • Run the query and confirm it returns results or blanks without a backend error.

Looks good, I did not run into any errors!

@emenslin emenslin requested a review from a team May 18, 2026 15:47
Copy link
Copy Markdown
Contributor

@Iwantexpresso Iwantexpresso left a comment

Choose a reason for hiding this comment

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

  • Run the query and confirm it returns results or blanks without a backend error.

no backend errors occurred. Nice!

@CarolineDenis CarolineDenis requested a review from a team May 19, 2026 10:57
Copy link
Copy Markdown
Collaborator

@bhumikaguptaa bhumikaguptaa left a comment

Choose a reason for hiding this comment

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

  • Run the query and confirm it returns results or blanks without a backend error.

Works as expected, no errors noticed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 📋Back Log

Development

Successfully merging this pull request may close these issues.

Query Builder crashes when linking any table to Projects table

4 participants