Skip to content

Commit 0c563eb

Browse files
authored
fix(db-postgres): querying on array wtihin a relationship field (#8152)
## Description Fixes #6037 - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change <!-- Please delete options that are not relevant. --> - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist: - [x] I have added tests that prove my fix is effective or that my feature works - [x] Existing test suite passes locally with my changes
1 parent df023a5 commit 0c563eb

File tree

5 files changed

+90
-4
lines changed

5 files changed

+90
-4
lines changed

packages/drizzle/src/queries/getTableColumnFromPath.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,13 @@ export const getTableColumnFromPath = ({
297297
`${tableName}_${tableNameSuffix}${toSnakeCase(field.name)}`,
298298
)
299299

300+
const arrayParentTable = aliasTable || adapter.tables[tableName]
301+
300302
constraintPath = `${constraintPath}${field.name}.%.`
301303
if (locale && field.localized && adapter.payload.config.localization) {
302304
joins.push({
303305
condition: and(
304-
eq(adapter.tables[tableName].id, adapter.tables[newTableName]._parentID),
306+
eq(arrayParentTable.id, adapter.tables[newTableName]._parentID),
305307
eq(adapter.tables[newTableName]._locale, locale),
306308
),
307309
table: adapter.tables[newTableName],
@@ -315,7 +317,7 @@ export const getTableColumnFromPath = ({
315317
}
316318
} else {
317319
joins.push({
318-
condition: eq(adapter.tables[tableName].id, adapter.tables[newTableName]._parentID),
320+
condition: eq(arrayParentTable.id, adapter.tables[newTableName]._parentID),
319321
table: adapter.tables[newTableName],
320322
})
321323
}

test/_community/payload-types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ export interface UserAuthOperations {
5454
*/
5555
export interface Post {
5656
id: string;
57-
customClientField?: string | null;
58-
customServerField?: string | null;
57+
text?: string | null;
58+
serverTextField?: string | null;
5959
richText?: {
6060
root: {
6161
type: string;

test/relationships/config.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,31 @@ export default buildConfigWithDefaults({
290290
},
291291
],
292292
},
293+
{
294+
slug: 'pages',
295+
fields: [
296+
{
297+
type: 'array',
298+
name: 'menu',
299+
fields: [
300+
{
301+
name: 'label',
302+
type: 'text',
303+
},
304+
],
305+
},
306+
],
307+
},
308+
{
309+
slug: 'rels-to-pages',
310+
fields: [
311+
{
312+
name: 'page',
313+
type: 'relationship',
314+
relationTo: 'pages',
315+
},
316+
],
317+
},
293318
],
294319
onInit: async (payload) => {
295320
await payload.create({

test/relationships/int.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
CustomIdNumberRelation,
1111
CustomIdRelation,
1212
Director,
13+
Page,
1314
Post,
1415
PostsLocalized,
1516
Relation,
@@ -676,6 +677,37 @@ describe('Relationships', () => {
676677
expect(query.docs).toHaveLength(1)
677678
expect(query.docs[0].id).toStrictEqual(firstLevelID)
678679
})
680+
681+
it('should allow querying within array nesting', async () => {
682+
const page = await payload.create({
683+
collection: 'pages',
684+
data: {
685+
menu: [
686+
{
687+
label: 'hello',
688+
},
689+
],
690+
},
691+
})
692+
693+
const rel = await payload.create({ collection: 'rels-to-pages', data: { page: page.id } })
694+
695+
const resEquals = await payload.find({
696+
collection: 'rels-to-pages',
697+
where: { 'page.menu.label': { equals: 'hello' } },
698+
})
699+
700+
expect(resEquals.totalDocs).toBe(1)
701+
expect(resEquals.docs[0].id).toBe(rel.id)
702+
703+
const resIn = await payload.find({
704+
collection: 'rels-to-pages',
705+
where: { 'page.menu.label': { in: ['hello'] } },
706+
})
707+
708+
expect(resIn.totalDocs).toBe(1)
709+
expect(resIn.docs[0].id).toBe(rel.id)
710+
})
679711
})
680712

681713
describe('Nested Querying Separate Collections', () => {

test/relationships/payload-types.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export interface Config {
2424
movieReviews: MovieReview;
2525
'polymorphic-relationships': PolymorphicRelationship;
2626
tree: Tree;
27+
pages: Page;
28+
'rels-to-pages': RelsToPage;
2729
users: User;
2830
'payload-preferences': PayloadPreference;
2931
'payload-migrations': PayloadMigration;
@@ -224,6 +226,31 @@ export interface Tree {
224226
updatedAt: string;
225227
createdAt: string;
226228
}
229+
/**
230+
* This interface was referenced by `Config`'s JSON-Schema
231+
* via the `definition` "pages".
232+
*/
233+
export interface Page {
234+
id: string;
235+
menu?:
236+
| {
237+
label?: string | null;
238+
id?: string | null;
239+
}[]
240+
| null;
241+
updatedAt: string;
242+
createdAt: string;
243+
}
244+
/**
245+
* This interface was referenced by `Config`'s JSON-Schema
246+
* via the `definition` "rels-to-pages".
247+
*/
248+
export interface RelsToPage {
249+
id: string;
250+
page?: (string | null) | Page;
251+
updatedAt: string;
252+
createdAt: string;
253+
}
227254
/**
228255
* This interface was referenced by `Config`'s JSON-Schema
229256
* via the `definition` "payload-preferences".

0 commit comments

Comments
 (0)