Skip to content

Fix formatted CO query results for nested relationship fields#7969

Open
acwhite211 wants to merge 8 commits into
mainfrom
issue-7919
Open

Fix formatted CO query results for nested relationship fields#7969
acwhite211 wants to merge 8 commits into
mainfrom
issue-7919

Conversation

@acwhite211
Copy link
Copy Markdown
Member

@acwhite211 acwhite211 commented Apr 13, 2026

Fixes #7919

Fixes incorrect query-builder results when CollectionObject is the base table and a display field uses a nested formatted relationship. Some formatted query result fields under CollectionObject were being parsed incorrectly from stored query stringid values.

In affected cases, relationship fields were being reinterpreted as tree-rank fields on the related table, which caused errors:

  • CO -> preparations -> prepType -> formatted could throw an error
  • CO -> determinations -> taxon -> formatted could appear blank
  • CO -> paleoContext -> chronosStrat/lithoStrat/... -> formatted could appear blank

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

  • Open the query builder and choose CollectionObject as the base table
  • Add a display column for preparations -> prepType -> formatted
  • Run the query and confirm results render instead of throwing an error
  • Add a display column for determinations -> taxon -> formatted
  • Run the query and confirm formatted taxon values are populated
  • Add a display column for paleoContext -> chronosStrat -> formatted
  • Run the query and confirm formatted chronostrat values are populated
  • Add a display column for paleoContext -> lithoStrat -> formatted if the collection has lithostrat data
  • Confirm previously working formatted fields still behave correctly, such as collectingEvent -> locality -> formatted
  • Confirm aggregated fields that were already working, such as accession agents, still return expected values

Summary by CodeRabbit

  • Bug Fixes

    • Fixed relationship-based query field parsing to properly resolve relationship fields and construct correct join paths.
  • Tests

    • Added multiple test cases to verify relationship field query specifications remain valid.
    • Re-enabled previously disabled relationship field roundtrip validation test.

Review Change Stack

@CarolineDenis CarolineDenis modified the milestones: 7.12.1, 7.12.2 Apr 21, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 18, 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: b9bfba0c-a24c-4ea0-8693-03622bcd8370

📥 Commits

Reviewing files that changed from the base of the PR and between 66b1266 and 301cfda.

📒 Files selected for processing (5)
  • specifyweb/backend/stored_queries/queryfieldspec.py
  • specifyweb/backend/stored_queries/tests/static/co_query_row_plan.py
  • specifyweb/backend/stored_queries/tests/static/simple_static_fields.py
  • specifyweb/backend/stored_queries/tests/test_execution/test_field_specs_from_json.py
  • specifyweb/backend/stored_queries/tests/tests_legacy.py

📝 Walkthrough

Walkthrough

This PR fixes relation-based stringid parsing in stored queries. The core logic in QueryFieldSpec.from_stringid() now correctly builds join paths for formatted relationship fields and resolves them to return the correct QueryFieldSpec, addressing issues where formatted results for certain table relationships were appearing blank or throwing errors.

Changes

Relation stringid parsing and test coverage

Layer / File(s) Summary
Core relation stringid resolution logic
specifyweb/backend/stored_queries/queryfieldspec.py
QueryFieldSpec.from_stringid() refactored to build join paths iteratively from parsed path elements and terminate early for relation stringids; dedicated is_relation resolution block determines the final relationship (preferentially from last join-path relationship or via field lookup), constructs QueryFieldSpec with date_part=None and tree_field=relation, and returns immediately.
Updated test fixtures reflecting corrected behavior
specifyweb/backend/stored_queries/tests/static/simple_static_fields.py, specifyweb/backend/stored_queries/tests/static/co_query_row_plan.py
static_simple_field_spec updated so cataloger, determinations, preparations, and collectingEvent fields target their joined model types (Agent, Determination, Preparation, CollectingEvent) with tree_field set to the relationship; locality spec removes TreeRankQuery and sets tree_field to CollectingEvent.locality; corresponding co_query_row_plan fixture indices adjusted for batch_edit_pack.
New test coverage and validation helper
specifyweb/backend/stored_queries/tests/test_execution/test_field_specs_from_json.py
assert_relation_stringid helper validates QueryFieldSpec stringid parsing/round-tripping, relationship status, join-path node names, and TreeRankQuery absence; four new relationship-preservation tests for prepType, collectingEvent, taxon, and chronosStrat verify correct join-path traversal; existing test_static_field_specs confirms fixture equality.
Legacy test re-enabled
specifyweb/backend/stored_queries/tests/tests_legacy.py
Removed expectedFailure import and decorator from test_stringid_roundtrip_en_masse to enable comprehensive stringid roundtrip validation as a normal assertion-based test.

Possibly related PRs

  • specify/specify7#8082: Both PRs modify QueryFieldSpec.from_stringid() in queryfieldspec.py, specifically changing how relation-based/formatted stringids consume join-path elements and avoid misinterpreting the final relationship as a tree-rank/tree-node lookup.

Suggested reviewers

  • emenslin
  • bhumikaguptaa
  • CarolineDenis
  • Iwantexpresso
🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Testing Instructions ⚠️ Warning Automated tests cover the affected components, but explicit documented testing instructions for manual QA are not present in the codebase despite being mentioned in PR objectives. Add documented step-by-step testing instructions showing how to verify the fix using the query builder with the specific formatted field cases from issue #7919.
✅ 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 accurately describes the main change: fixing query results for nested relationship fields in CollectionObject queries.
Linked Issues check ✅ Passed The code changes directly address the reported issues by rewriting QueryFieldSpec.from_stringid() to correctly resolve nested relationship fields and adding comprehensive unit tests to verify the fix.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing nested relationship field parsing and updating related tests; no unrelated modifications were introduced.
Automatic Tests ✅ Passed PR includes automatic tests: 4 unit tests for nested relationship stringids, helper method, re-enabled roundtrip test, updated test fixtures covering all issue scenarios.

✏️ 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-7919

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 21:34
Triggered by 1400132 on branch refs/heads/issue-7919
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 results render instead of throwing an error
  • Run the query and confirm formatted taxon values are populated
  • Run the query and confirm formatted chronostrat values are populated
  • Confirm previously working formatted fields still behave correctly, such as collectingEvent -> locality -> formatted
  • Confirm aggregated fields that were already working, such as accession agents, still return expected values

Looks good, I didn't run into any issues

@emenslin emenslin requested a review from a team May 19, 2026 14:41
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 results render instead of throwing an error
  • Run the query and confirm formatted taxon values are populated
  • Run the query and confirm formatted chronostrat values are populated
  • Confirm previously working formatted fields still behave correctly, such as collectingEvent -> locality -> formatted
  • Confirm aggregated fields that were already working, such as accession agents, still return expected values

Looks good to me.

@Iwantexpresso Iwantexpresso requested a review from a team May 19, 2026 16:45
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 results render instead of throwing an error
  • Run the query and confirm formatted taxon values are populated
  • Run the query and confirm formatted chronostrat values are populated
  • Confirm previously working formatted fields still behave correctly, such as collectingEvent -> locality -> formatted
  • Confirm aggregated fields that were already working, such as accession agents, still return expected values

every results is being displayed as intended, nice work!

Image

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.

Some table formats not working with CO as base table

5 participants