Skip to content

Commit 850cc38

Browse files
authored
fix(db-postgres): hasMany relationship/number/text fields inside blocks are incorrectly returned when using select (#14399)
Fixes #14157
1 parent 118d005 commit 850cc38

File tree

4 files changed

+187
-41
lines changed

4 files changed

+187
-41
lines changed

packages/drizzle/src/find/traverseFields.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
type SelectType,
1515
type Where,
1616
} from 'payload'
17-
import { fieldIsVirtual, fieldShouldBeLocalized } from 'payload/shared'
17+
import { blocks, fieldIsVirtual, fieldShouldBeLocalized } from 'payload/shared'
1818
import toSnakeCase from 'to-snake-case'
1919

2020
import type { BuildQueryJoinAliases, DrizzleAdapter } from '../types.js'
@@ -91,6 +91,7 @@ type TraverseFieldArgs = {
9191
depth?: number
9292
draftsEnabled?: boolean
9393
fields: FlattenedField[]
94+
forceWithFields?: boolean
9495
joinQuery: JoinQuery
9596
joins?: BuildQueryJoinAliases
9697
locale?: string
@@ -119,6 +120,7 @@ export const traverseFields = ({
119120
depth,
120121
draftsEnabled,
121122
fields,
123+
forceWithFields,
122124
joinQuery = {},
123125
joins,
124126
locale,
@@ -231,6 +233,7 @@ export const traverseFields = ({
231233
depth,
232234
draftsEnabled,
233235
fields: field.flattenedFields,
236+
forceWithFields,
234237
joinQuery,
235238
locale,
236239
parentIsLocalized: parentIsLocalized || field.localized,
@@ -358,6 +361,7 @@ export const traverseFields = ({
358361
depth,
359362
draftsEnabled,
360363
fields: block.flattenedFields,
364+
forceWithFields: blockSelect === true,
361365
joinQuery,
362366
locale,
363367
parentIsLocalized: parentIsLocalized || field.localized,
@@ -400,6 +404,7 @@ export const traverseFields = ({
400404
depth,
401405
draftsEnabled,
402406
fields: field.flattenedFields,
407+
forceWithFields,
403408
joinQuery,
404409
joins,
405410
locale,
@@ -862,6 +867,23 @@ export const traverseFields = ({
862867
}
863868

864869
default: {
870+
if (forceWithFields) {
871+
if (
872+
(field.type === 'relationship' || field.type === 'upload') &&
873+
(field.hasMany || Array.isArray(field.relationTo))
874+
) {
875+
withTabledFields.rels = true
876+
}
877+
878+
if (field.type === 'number' && field.hasMany) {
879+
withTabledFields.numbers = true
880+
}
881+
882+
if (field.type === 'text' && field.hasMany) {
883+
withTabledFields.texts = true
884+
}
885+
}
886+
865887
if (!select && !selectAllOnCurrentLevel) {
866888
break
867889
}

test/select/getConfig.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,34 @@ export const getConfig: () => Partial<Config> = () => ({
3737
},
3838
{
3939
slug: 'rels',
40-
fields: [],
40+
fields: [{ type: 'text', name: 'text' }],
41+
},
42+
{
43+
slug: 'relationships-blocks',
44+
fields: [
45+
{
46+
type: 'blocks',
47+
name: 'blocks',
48+
blocks: [
49+
{
50+
slug: 'block',
51+
fields: [
52+
{
53+
type: 'relationship',
54+
name: 'hasMany',
55+
relationTo: 'rels',
56+
hasMany: true,
57+
},
58+
{
59+
type: 'relationship',
60+
name: 'hasOne',
61+
relationTo: 'rels',
62+
},
63+
],
64+
},
65+
],
66+
},
67+
],
4168
},
4269
CustomID,
4370
UsersCollection,

test/select/int.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,6 +2478,57 @@ describe('Select', () => {
24782478
array,
24792479
})
24802480
})
2481+
2482+
it('should properly return relationships when using select on block with depth 0', async () => {
2483+
const rel_1 = await payload.create({ collection: 'rels', data: { text: 'rel-1' } })
2484+
const doc = await payload.create({
2485+
collection: 'relationships-blocks',
2486+
data: {
2487+
blocks: [
2488+
{
2489+
blockType: 'block',
2490+
hasMany: [rel_1],
2491+
hasOne: rel_1,
2492+
},
2493+
],
2494+
},
2495+
})
2496+
const result = await payload.findByID({
2497+
depth: 0,
2498+
collection: 'relationships-blocks',
2499+
id: doc.id,
2500+
select: { blocks: true },
2501+
})
2502+
2503+
expect(result.blocks[0]?.hasOne).toBe(rel_1.id)
2504+
expect(result.blocks[0]?.hasMany).toEqual([rel_1.id])
2505+
})
2506+
2507+
it('should populate relationships when using select on block', async () => {
2508+
const rel_1 = await payload.create({ collection: 'rels', data: { text: 'rel-1' } })
2509+
const doc = await payload.create({
2510+
collection: 'relationships-blocks',
2511+
data: {
2512+
blocks: [
2513+
{
2514+
blockType: 'block',
2515+
hasMany: [rel_1],
2516+
hasOne: rel_1,
2517+
},
2518+
],
2519+
},
2520+
})
2521+
2522+
const result = await payload.findByID({
2523+
depth: 1,
2524+
collection: 'relationships-blocks',
2525+
id: doc.id,
2526+
select: { blocks: true },
2527+
})
2528+
2529+
expect(result.blocks[0]?.hasOne.text).toBe('rel-1')
2530+
expect(result.blocks[0]?.hasMany[0].text).toBe('rel-1')
2531+
})
24812532
})
24822533

24832534
async function createPost() {

0 commit comments

Comments
 (0)