Let a view sort order by one field of its related record

FE
13h ago
+1001-111

Adds viewSort.relationTargetFieldMetadataId, so a sort on a many-to-one relation can order records by one field of the related record, e.g. People sorted by Company › Employees. It mirrors viewField.relationTargetFieldMetadataId (#30472) and viewFilter.relationTargetFieldMetadataId.

This is the server half of sorting a lookup column from its header. The query engine already orders through to-one relations (orderBy: [{ company: { employees: AscNullsLast } }], with cursor pagination carrying the joined value). What was missing is a way to store such a sort on a view. Nothing in the app sets it until the front PR in the Stack section lands.

Changes

  • Schema: a nullable FK to fieldMetadata, ON DELETE CASCADE, with a partial index, like the view field and view filter columns. Fast instance command 2.60.0_AddViewSortRelationTargetFieldMetadataIdFastInstanceCommand_1794927394074.
  • Validation (view-sort validator, on create and update, through the new validateFlatViewSortRelationTarget):
    • only on a sort whose field is a plain many-to-one relation (morph relations rejected);
    • the target must belong to the relation's target object and be active;
    • the target must not be a relation (one hop).
  • Lifecycle: deactivating the target field removes the sort instead of leaving a dangling id; deleting it cascades.
  • Public surface: ViewSort.relationTargetFieldMetadataId on the type and on the create and update inputs; null on update clears it. App manifests carry it as relationTargetFieldMetadataUniversalIdentifier (documented in layout/views.mdx).
  • upsertViewWidget sets the new property to null. The view-sort AI tools don't take it yet, as with view fields before #30450.

Unlike view fields, there is no application-ownership rule: view sorts have no overridable properties, so a workspace edit to an application's sort already behaves the same for every property.

Stack

PRWhat it doesBuilds on
#30439Front: lookup columns, added from the + menumain
#30653Front: filter a lookup column from its headermerged into #30439
👉 this PRServer: viewSort.relationTargetFieldMetadataIdmain
#30661Front: sort a lookup column from its header#30439 + this PR

Size: code +301 −22, tests and generated files +572 −4 (M).

Testing

  • Unit: validateFlatViewSortRelationTarget (9 cases), field deactivation removing a sort that targets the field, and the converter, reconstruction and export specs. The flat-field-metadata, flat-view-sort, flat-entity, view, view-sort, field-metadata, application and workspace-migration suites pass (299 suites, 1,921 tests, 84 snapshots).
  • Integration: new view-sort-relation-target.integration-spec.ts, modelled on the view field one. It couldn't run locally (the dev machine ran out of memory), so CI is its first run. The same flows were checked by hand against a server built from this branch:
    • creating a sort on person.company with target company.employees returns the target; updating it to null clears it;
    • creating or updating with a non-relation field, a field of another object, or a relation target is rejected with the matching message;
    • deactivating company.employees removes the sort;
    • orderBy: [{ company: { employees: DescNullsLast } }] returns records.
  • Server typecheck and oxlint; the client SDK and front generated-metadata were regenerated against a server built from this branch.


Generated by Claude Code


Generated by Claude Code