Relations Field Ordering & Pagination#
Overview#
RelationsField is the Content Manager component that handles multi-entry relation fields (many-to-many, one-to-many, etc.) in the edit view. It combines two concerns that interact in subtle ways:
- Paginated loading ā relations are fetched 5 at a time (
RELATIONS_TO_DISPLAY = 5) via a "Load More" button - Drag-and-drop reordering ā the displayed list is sorted by fractional-indexing keys (
__temp_key__) and changes are diffed against server state to build aconnectarray with positional anchors (before,after,start,end)
Key source files:
| Layer | File |
|---|---|
| UI component | Relations.tsx |
| RTK Query service | services/relations.ts |
| Backend orderer | relations-orderer.ts |
Pagination State & RTK Query Cache Merge#
RelationsField tracks which page has been loaded in a local useState(1) . Each "Load More" click increments it by 1 .
The getRelations RTK Query endpoint uses a custom serializeQueryArgs that excludes page from the cache key, so all pages of the same field share a single cache entry. A merge function controls how pages accumulate:
- page > current cached page ā prepend new items to accumulated results (relations display newest first)
- page === 1 ā wipe the entire cache and replace with the first page only
This design means any re-render that resets currentPage to 1 will trigger a full cache wipe on the next RTK Query fetch, discarding all previously loaded pages.
Load More Reset Bug (Issue #27199)#
Symptom: After loading more than 5 relations and opening then closing a relation entry modal, the list reverts to 5 entries and "Load More" disappears or never loads more .
Root cause chain:
handleCloseModalcallstriggerRefetchDocumentto refresh the parent document- The refetch causes
RelationsFieldto re-render;currentPageresets to1 - RTK Query refires
getRelationswithpage: 1 - The cache
mergefunction detectspage === 1and wipes all accumulated results ā only 5 entries remain
There is a secondary aggravator: the isSubmitting useEffect unconditionally calls setCurrentPage(1) on every state change, including initial mount .
Status of fix ā PR #27466 (open): The proposed fix reads currentPage initial value from relationsApi.endpoints.getRelations.useQueryState(...) so the component can survive a re-render with the correct page. It also narrows the isSubmitting effect to fire only when isSubmitting becomes true .
Systemic issue: triggerRefetchDocument on every clean modal close is a known workaround. An inline TODO in the codebase notes that the correct fix is replacing it with RTK Query invalidatesTags .
Drag-and-Drop Reorder ā Backend Ordering Bugs#
Wrong Position on Save (regression since v5.51.0) ā PR #27444#
Symptom: Dragging a relation to a new position saves it at the wrong position ā e.g., dragging to position 10 in a list of 100 ends up at position 6. start and end drags are unaffected; the bug manifests whenever a relation is dropped next to another, which is the majority of drag operations .
Root cause: relationsOrderer is initialized with a partial list ā only the anchor rows named in the payload plus the lowest- and highest-order boundary rows. The midpoint interpolation introduced in PR #26112 used computedRelations[beforeIdx - 1] (the neighbor in the partial array) as the other side of the midpoint, but that neighbor is the lowest-order boundary row, not the actual adjacent stored row. The computed midpoint therefore lands far from the intended position .
Fix (PR #27444, merged Aug 24 2026): Place the relation strictly relative to the anchor's own order value: r.order = relation.order - 0.5 for before, r.order = relation.order + 0.5 for after. A documentation comment was added to relationsOrderer to capture the partial-list constraint. Shared order values are harmless: getOrderMap() distributes collisions into fractional slots and cleanOrderColumns renumbers the join table with ROW_NUMBER() .
Reorder-to-Start Ignored After Save ā PR #26112#
Symptom: Dragging a relation to position 1 (the top) did not persist ā after saving and clicking "Load More", the item appeared at position 2 .
Root causes:
- The frontend sent
{ before: firstItemDocId }forcurrentIndex === 0, but that item might not be inrelationsOrderer'sinitArr order: 0was treated as falsy and silently defaulted to1updateRelationsonly fetched the max-order boundary row, giving the orderer no anchor for a{ start: true }placement
Fix (PR #26112, merged Jul 2026):
- Frontend:
Relations.tsxnow emits{ start: true }when the dragged item lands at index 0 - Backend:
updateRelationsfetches both min-order and max-orderadjacentRelations;{ start: true }assignsfirstRelation.order - 0.5;order: 0is now guarded with a null/undefined check
Note: PR #26112's
before/aftermidpoint interpolation introduced the regression that PR #27444 subsequently fixed.
Key Constraints & Gotchas#
-
initArris always partial.relationsOrdererreceives only anchor rows plus boundary rows fromupdateRelations. Neighbors incomputedRelationsare not neighbors in storage; all placement math must be relative to the anchor's ownorder, never to its neighbors in the partial array . -
currentPageis not durable. It is plain component state and resets on any re-render triggered by a parent document refetch. The Load More bug (#27199) and its fix (PR #27466) both hinge on this . -
__temp_key__is the display-order source of truth. Relations must be sorted by this fractional-indexing key in the UI ā not byidor server order ā otherwise drag-and-drop produces the wrong visual order . -
ONE_WAY_RELATIONS cannot be reordered.
oneWay,oneToOne,manyToOne,oneToManyMorph, andoneToOneMorphrelations only ever hold one entry;canReorderisfalsefor these types . -
triggerRefetchDocumenton modal close is a known workaround. The ideal replacement is RTK QueryinvalidatesTags, which would avoid the state-loss side effects entirely .