Skip to content

fix(query): refuse ordering by a relationship that holds no column - #1023

Merged
abnegate merged 1 commit into
mainfrom
fix-order-columnless-relationship
Oct 15, 2026
Merged

abnegate merged 1 commit into
mainfrom
fix-order-columnless-relationship

Conversation

@abnegate

Copy link
Copy Markdown
Member

Problem

A one-to-many parent, a many-to-one child and either side of a many-to-many keep their related ids on the other collection or in a junction table. Their own collection has no column to sort on. The order validator accepted every relationship key anyway. The engine then failed on the unknown column, and find() and count() surfaced it as:

Utopia\Database\Exception\NotFound: Attribute not found

Appwrite answered that as a 404 for the whole collection. appwrite/appwrite#14543 works around it with its own Validator\Query\Order. This moves the check into the library so that class can go.

Fix

The Order validator now marks the relationship keys that hold no column, using Joined\Collection::columns(). That function already knows which side of each relationship stores its column. An orderAsc() or orderDesc() on one of those keys is refused with the QueryException every other invalid order raises:

Invalid query: Cannot order by relationship attribute: <key>

Relationships that hold a column (a one-to-many child, a many-to-one parent, a one-to-one parent, a two-way one-to-one child) order as before.

Tests

tests/unit/RelationshipOrderTest.php runs on SQLite and covers:

  • Refused: a one-to-many parent, both sides of a many-to-many, and a many-to-one child, for orderAsc and orderDesc, through find() and count().
  • Read as before: a one-to-many child, a one-to-one parent, a two-way one-to-one child, and a many-to-one parent.

On main, the four refused cases fail with NotFound: Attribute not found. They pass with the fix. The validator unit suite (964 tests) and the relationship/order E2Es on SQLite (243 tests) pass. Pint and PHPStan are clean on the changed files.

🤖 Generated with Claude Code

A one-to-many parent, a many-to-one child and either side of a
many-to-many keep their related ids on the other collection or in a
junction table, so their collection has no column to order by. The order
validator accepted every relationship key, the engine then failed on the
unknown column, and the read surfaced that as NotFound "Attribute not
found", which a consumer answers as a missing collection.

The validator now marks those keys from Joined\Collection::columns(),
which already tells which side of a relationship stores its column, and
refuses an orderAsc() or orderDesc() on them with "Cannot order by
relationship attribute: <key>", the QueryException every other invalid
order raises. Relationships that hold a column order as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 57 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: utopia-php/database/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1c445b3d-1c3b-42f5-8e82-4b3cc062bc1c
📥 Commits

Reviewing files that changed from the base of the PR and between 0555ec9 and 94f99ee.

📒 Files selected for processing: 2
  • src/Database/Validator/Query/Order.php
  • tests/unit/RelationshipOrderTest.php
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@abnegate
abnegate merged commit ba8a5fe into main Oct 15, 2026
24 checks passed
@abnegate
abnegate deleted the fix-order-columnless-relationship branch October 15, 2026 11:27
ArnabChatterjee20k pushed a commit to ArnabChatterjee20k/appwrite that referenced this pull request Oct 15, 2026
…he library

countRows() re-ran a grouped, distinct or aggregate read for up to
APP_LIMIT_COUNT rows because count() ignored groupBy, distinct and
aggregates. utopia-php/database#1020 counts them by their rows, so the
total goes back to count() with APP_LIMIT_COUNT.

The relationship order validator and its unit tests go too:
utopia-php/database#1023 refuses ordering by a relationship that holds no
column with the same message, as a QueryException the list already
answers with 400 general_query_invalid.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant