Skip to content

fix(drizzle): use table alias when sorting joined drafts by a localized field#17043

Open
yashs33244 wants to merge 2 commits into
payloadcms:mainfrom
yashs33244:fix/localize-status-draft-join-sql
Open

fix(drizzle): use table alias when sorting joined drafts by a localized field#17043
yashs33244 wants to merge 2 commits into
payloadcms:mainfrom
yashs33244:fix/localize-status-draft-join-sql

Conversation

@yashs33244

Copy link
Copy Markdown

What?

Fixes invalid SQL (invalid reference to FROM-clause entry for table "_r_v") when querying a collection that joins a localized draft collection whose default sort is a localized field (for example _status when localizeStatus is on), with a locale set. Postgres-specific.

Why?

buildOrderBy runs against the join subquery's aliased version table, but it called getTableColumnFromPath without passing aliasTable. The where builder (parseParams) already passes it. For a localized sort column, getTableColumnFromPath builds the locales JOIN condition from (aliasTable || adapter.tables[tableName]).id; with aliasTable omitted it referenced the canonical, un-aliased version table, which isn't in the subquery's FROM clause - so Postgres rejected the query.

How?

Pass aliasTable into the getTableColumnFromPath call in buildOrderBy, matching parseParams. The locales JOIN now references the aliased version table that's actually in scope.

Added an integration test in the localizeStatus suite (a join onto a draft collection sorted by _status) that fails on Postgres before the change and passes after.

Fixes #15248

The bug is a Postgres FROM-clause error; it can't occur on MongoDB, and
seeding a fresh versions collection inside a txn there is racy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL query error when fetching collection using the latest localization parameters

1 participant