Skip to content

fix(drizzle): sort by join field on versioned collection with custom text id#16391

Open
nathanbowang wants to merge 2 commits intopayloadcms:mainfrom
nathanbowang:fix/sort-by-join-on-versioned-collection-with-custom-text-id
Open

fix(drizzle): sort by join field on versioned collection with custom text id#16391
nathanbowang wants to merge 2 commits intopayloadcms:mainfrom
nathanbowang:fix/sort-by-join-on-versioned-collection-with-custom-text-id

Conversation

@nathanbowang
Copy link
Copy Markdown
Contributor

What?

Fix a Postgres crash when sorting the admin list view by a join field on a collection that has both versions: { drafts: true } enabled and a custom text id (e.g. nanoid).

Why?

In that combination the query fails with operator does not exist: integer = character varying, the broken sort is persisted into payload_preferences, and the user cannot recover from the admin UI — the only fix today is to manually edit the database.

How?

In getTableColumnFromPath.ts, both branches of case 'join' built the join condition off adapter.tables[*].id. On a versions table (*_v) id is the version-row PK (integer), not the parent document id — but the joined column actually references the parent document id, which is varchar for custom text ids.

Use the versions table's parent column (mapped from parent_id) as the join key whenever the source table is a versions table. Both branches need the same fix; otherwise only some shapes of on are repaired.

Regression test added under test/joins, exercising both code paths.

Fixes #16390

Made with Cursor

…text id

When the list view is sorted by a `join` field on a collection that has both
`versions` enabled and a custom text id (e.g. nanoid), the query crashes with:

  operator does not exist: integer = character varying

`getTableColumnFromPath` builds the join condition using the versions table's
`id` column. On a versions table, `id` is the versions row PK (always integer),
not the parent document id. The joined `_rels` / target table column references
the parent document id, which is `varchar` for custom text ids, so the types
do not match.

Use the versions table's `parent` column (mapped from the `parent_id` db
column, which is the actual reference to the parent document id) instead of
`id` whenever the source table is a versions table. Fixes both branches of
`case 'join'` (single-segment `on` with `hasMany`, and the non-`hasMany` path).

Made-with: Cursor
… text id

Adds a regression test that creates a versioned collection with a custom
text id and `join` fields (covering both the hasMany and non-hasMany
branches in `getTableColumnFromPath`), then runs `payload.find` with
`draft: true` and `sort` set to the join field.

Without the fix this fails with:

  operator does not exist: character varying = integer

Made-with: Cursor
@nathanbowang nathanbowang force-pushed the fix/sort-by-join-on-versioned-collection-with-custom-text-id branch from 177a48b to 0407620 Compare April 27, 2026 05:11
@nathanbowang
Copy link
Copy Markdown
Contributor Author

The 3 failing checks all look unrelated to this PR (which only touches packages/drizzle/src/queries/getTableColumnFromPath.ts and adds a test/joins regression test):

  • int-sqlite-uuidv7 (1/3) — failed at the Start services step (CI service container startup), Integration Tests never ran. Infra issue.
  • E2E - lexical__collections__LexicalJSXConverterLexical JSX Converter › indents should be 40px in the editor and in the jsx converter timed out on all 5 retries with locator.focus: Test timeout of 60000ms exceeded, alongside MongoServerError: TransientTransactionError on _array-fields_versions. Looks flaky / mongo transaction contention.
  • E2E - lexical__collections__RichText — the whole job hit the 45m execution-time limit and got cancelled.
  • All Green — fails as an aggregate of the above.

Could a maintainer please rerun the failed jobs? I don't have permissions to do so on this fork. Happy to push an empty commit to retrigger if preferred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Postgres] sort by join field crashes on versioned collection with custom text id (integer = character varying)

1 participant