Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/drizzle/src/queries/buildOrderBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const buildOrderBy = ({
try {
const { columnName: sortTableColumnName, table: sortTable } = getTableColumnFromPath({
adapter,
aliasTable,
collectionPath: sortProperty,
fields,
joins,
Expand Down
18 changes: 18 additions & 0 deletions test/localization/localizeStatus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ export default buildConfigWithDefaults({
// Explicitly disabled — migration should skip collections without versions
versions: false,
},
// Joined draft collection sorted by a localized field (`_status`) — repros #15248.
{
slug: 'joinOrgs',
fields: [
{ name: 'title', type: 'text', localized: true },
{ name: 'repos', type: 'join', collection: 'joinRepos', on: 'org' },
],
versions: { drafts: true },
},
{
slug: 'joinRepos',
defaultSort: ['_status', '-updatedAt'],
fields: [
{ name: 'org', type: 'relationship', relationTo: 'joinOrgs' },
{ name: 'title', type: 'text', localized: true },
],
versions: { drafts: true },
},
],
localization: {
defaultLocale: 'en',
Expand Down
44 changes: 43 additions & 1 deletion test/localization/localizeStatus.int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { sql as drizzleSql } from 'drizzle-orm'
import { Types } from 'mongoose'
import path from 'path'
import { fileURLToPath } from 'url'
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest'
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'

import { initPayloadInt } from '../__helpers/shared/initPayloadInt.js'

Expand All @@ -28,6 +28,48 @@ describe('localizeStatus migration', () => {
}
})

// SQL-adapter only: the bug is a Postgres FROM-clause error in the join ORDER BY, which
// cannot occur on MongoDB (no SQL join subquery there).
describe.skipIf(process.env.PAYLOAD_DATABASE === 'mongodb')(
'querying a joined localized draft collection',
() => {
const createdOrgIDs: Array<number | string> = []

afterEach(async () => {
for (const id of createdOrgIDs) {
await payload.delete({ id, collection: 'joinOrgs' })
}
createdOrgIDs.length = 0
})

// Used to throw `invalid reference to FROM-clause entry for table "_r_v"` on Postgres
// because the join subquery's ORDER BY referenced the un-aliased version table.
it('should not throw when the joined collection sorts by a localized field', async () => {
const org = await payload.create({
collection: 'joinOrgs',
data: { title: 'Acme' },
locale: 'en',
})
createdOrgIDs.push(org.id)

await payload.create({
collection: 'joinRepos',
data: { org: org.id, title: 'repo-a' },
locale: 'en',
})

const result = await payload.find({
collection: 'joinOrgs',
draft: true,
locale: 'en',
})

expect(result.docs).toHaveLength(1)
expect(result.docs[0]?.repos?.docs).toHaveLength(1)
})
},
)

describe.skipIf(process.env.PAYLOAD_DATABASE !== 'postgres')('PostgreSQL', () => {
// Reset both test collections to their pre-migration database shape before every
// scenario so each test is self-contained and order-independent. Real users' databases
Expand Down
Loading