Let a view sort order by one field of its related record
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 command2.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.relationTargetFieldMetadataIdon the type and on the create and update inputs;nullon update clears it. App manifests carry it asrelationTargetFieldMetadataUniversalIdentifier(documented inlayout/views.mdx). upsertViewWidgetsets the new property tonull. 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
| PR | What it does | Builds on |
|---|---|---|
| #30439 | Front: lookup columns, added from the + menu | main |
| #30653 | Front: filter a lookup column from its header | merged into #30439 |
| 👉 this PR | Server: viewSort.relationTargetFieldMetadataId | main |
| #30661 | Front: 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
nullclears 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.
- creating a sort on person.company with target company.employees returns the target; updating it to
- Server typecheck and oxlint; the client SDK and front
generated-metadatawere regenerated against a server built from this branch.
Generated by Claude Code
Generated by Claude Code