Skip to content

Commit eee6432

Browse files
authored
fix(db-postgres): query has many relationships nested in row fields (#9944) (#9944)
### What? Querying by nested to rows fields in has many relationships like this: ```ts const result = await payload.find({ collection: 'relationship-fields', where: { 'relationToRowMany.title': { equals: 'some-title' }, }, }) ``` Where the related collection: ```ts const RowFields: CollectionConfig = { slug: rowFieldsSlug, fields: [ { type: 'row', fields: [ { name: 'title', label: 'Title within a row', type: 'text', required: true, }, ], }, ], } ``` was broken ### Why? We migrated to use `flattenedFields`, but not in this specific case. This error would be caught earlier we used `noImplictAny` typescript rule. https://www.typescriptlang.org/tsconfig/#noImplicitAny which wouldn't allow us to create variable like this: ```ts let relationshipFields // relationshipFields is any here ``` Instead, we should write: ```ts let relationshipFields: FlattenedField[] ``` We should migrate to it and `strictNullChecks` as well. Fixes #9534
1 parent 044c22d commit eee6432

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

packages/drizzle/src/queries/getTableColumnFromPath.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ export const getTableColumnFromPath = ({
358358
const newCollectionPath = pathSegments.slice(1).join('.')
359359

360360
if (Array.isArray(field.relationTo) || field.hasMany) {
361-
let relationshipFields
361+
let relationshipFields: FlattenedField[]
362362
const relationTableName = `${rootTableName}${adapter.relationshipsSuffix}`
363363
const {
364364
newAliasTable: aliasRelationshipTable,
@@ -405,7 +405,7 @@ export const getTableColumnFromPath = ({
405405
newTableName = adapter.tableNameMap.get(toSnakeCase(relationshipConfig.slug))
406406

407407
// parent to relationship join table
408-
relationshipFields = relationshipConfig.fields
408+
relationshipFields = relationshipConfig.flattenedFields
409409
;({ newAliasTable } = getTableAlias({ adapter, tableName: newTableName }))
410410

411411
joins.push({

test/fields/collections/Relationship/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ const RelationshipFields: CollectionConfig = {
115115
minRows: 2,
116116
type: 'relationship',
117117
},
118+
{
119+
name: 'relationToRow',
120+
relationTo: 'row-fields',
121+
type: 'relationship',
122+
},
123+
{
124+
name: 'relationToRowMany',
125+
relationTo: 'row-fields',
126+
type: 'relationship',
127+
hasMany: true,
128+
},
118129
],
119130
slug: relationshipFieldsSlug,
120131
}

test/fields/int.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,37 @@ describe('Fields', () => {
453453
expect(result.docs).toHaveLength(1)
454454
expect(result.docs[0]).toMatchObject(relationshipInArray)
455455
})
456+
457+
it('should query text in row after relationship', async () => {
458+
const row = await payload.create({
459+
collection: 'row-fields',
460+
data: { title: 'some-title', id: 'custom-row-id' },
461+
})
462+
const textDoc = await payload.create({
463+
collection: 'text-fields',
464+
data: { text: 'asd' },
465+
})
466+
467+
const rel = await payload.create({
468+
collection: 'relationship-fields',
469+
data: {
470+
relationship: { relationTo: 'text-fields', value: textDoc },
471+
relationToRow: row.id,
472+
relationToRowMany: [row.id],
473+
},
474+
})
475+
476+
const result = await payload.find({
477+
collection: 'relationship-fields',
478+
where: {
479+
'relationToRow.title': { equals: 'some-title' },
480+
'relationToRowMany.title': { equals: 'some-title' },
481+
},
482+
})
483+
484+
expect(result.docs[0].id).toBe(rel.id)
485+
expect(result.totalDocs).toBe(1)
486+
})
456487
})
457488

458489
describe('timestamps', () => {

test/fields/payload-types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,8 @@ export interface RelationshipField {
13351335
value: string | TextField;
13361336
}[]
13371337
| null;
1338+
relationToRow?: (string | null) | RowField;
1339+
relationToRowMany?: (string | RowField)[] | null;
13381340
updatedAt: string;
13391341
createdAt: string;
13401342
}
@@ -2970,6 +2972,8 @@ export interface RelationshipFieldsSelect<T extends boolean = true> {
29702972
id?: T;
29712973
};
29722974
relationshipWithMinRows?: T;
2975+
relationToRow?: T;
2976+
relationToRowMany?: T;
29732977
updatedAt?: T;
29742978
createdAt?: T;
29752979
}

0 commit comments

Comments
 (0)