db-postgres: after reordering array rows, a row without a polymorphic relationship inherits the one stored in _rels at its new index

Open
#19,659 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@pcbeingused333 is already working on this.

Since Nov 17, 2026.

  • #19687 by @pcbeingused333 — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
postgresql, typescript
Domain
database

Research direction

The issue identifies @payloadcms/drizzle's upsertRow as the affected entry point. Read how it updates positional paths in the Postgres _rels table, then reproduce the reorder with repro-5.ts against an empty PostgreSQL database. It is done when the saved _rels rows contain only relationships present in the data and Login has no inherited reference.

Written by the indexing model from the issue text.

Description

Bug db-postgres (`@payloadcms/drizzle` `upsertRow`) Payload OSS
Describe the Bug

In the Postgres adapter, relationships inside arrays are stored in <table>_rels under positional paths (links.0.reference, links.1.reference). On update, upsertRow deletes the existing rels only for the paths it re-inserts. A row whose relationship is absent from the data never clears the rel at its index. In the reproduction, that is a polymorphic relationTo: ['notes'] field shown only for type: 'internal'. After a reorder, such a row inherits the relationship of the row that used to be at its position.

A single-collection relationTo: 'notes' is stored as a column on the array table, so it is not affected. Only relationships that go through _rels are affected: polymorphic ones, and presumably hasMany ones.

Expected: after the update, _rels holds only the relationships present in the saved data, and the moved row's reference is empty.

Link to the code that reproduces this issue

https://github.com/elghaied/payload-v4-reproductions (repro-5.ts)

Reproduction Steps
  1. pnpm install, then cp .env.example .env and point DATABASE_URL at an empty PostgreSQL database
  2. pnpm repro repro-5.ts

The script creates a menu with [First (internal, reference → a note), Login (external, no reference)], then saves [Login, First] with the same ids, First keeping its reference. Output:

menus_rels rows: [{"path":"links.0.reference","notes_id":3},{"path":"links.1.reference","notes_id":3}]
observed Login.reference: { relationTo: 'notes', value: 3 }  expected: null/undefined
Which area(s) are affected?

db-postgres (@payloadcms/drizzle upsertRow)

Environment Info
payload: 4.0.0-beta.9
@payloadcms/db-postgres: 4.0.0-beta.9
Node.js: 24.22.1
PostgreSQL: 17.10
(local API only, no Next.js app)
Dominant language
TypeScript
Stars
45.8k
Forks
4.4k
Avg merge
2d 16h
Merged PRs (30d)
251

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from payloadcms/payload

All issues in payloadcms/payload

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.