Skip to content

Commit 9b00b59

Browse files
authored
fix: corrects cases of false positive identification of custom id fields (#9245)
This PR fixes cases where you may have a field called `id` within a group or a named tab, which would have incorrectly been treated as a custom ID field for the collection. However, custom IDs need to be defined at the root level - and now Payload only respects custom IDs defined at the root level.
1 parent 1393d84 commit 9b00b59

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

packages/payload/src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,13 @@ export class BasePayload {
581581
this.config.collections.forEach((collection) => {
582582
let customIDType = undefined
583583
const findCustomID: TraverseFieldsCallback = ({ field, next }) => {
584-
if (['array', 'blocks'].includes(field.type)) {
585-
next()
586-
return
584+
if (
585+
['array', 'blocks', 'group'].includes(field.type) ||
586+
(field.type === 'tab' && 'name' in field)
587+
) {
588+
return true
587589
}
590+
588591
if (!fieldAffectsData(field)) {
589592
return
590593
}

test/database/config.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,46 @@ export default buildConfigWithDefaults({
383383
],
384384
},
385385
},
386+
{
387+
name: 'title',
388+
type: 'text',
389+
},
386390
],
387391
versions: { drafts: true },
388392
},
393+
{
394+
slug: 'fake-custom-ids',
395+
fields: [
396+
{
397+
name: 'title',
398+
type: 'text',
399+
},
400+
{
401+
name: 'group',
402+
type: 'group',
403+
fields: [
404+
{
405+
name: 'id',
406+
type: 'text',
407+
},
408+
],
409+
},
410+
{
411+
type: 'tabs',
412+
tabs: [
413+
{
414+
name: 'myTab',
415+
fields: [
416+
{
417+
name: 'id',
418+
type: 'text',
419+
},
420+
],
421+
},
422+
],
423+
},
424+
],
425+
},
389426
{
390427
slug: 'relationships-migration',
391428
fields: [

test/database/int.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,46 @@ describe('database', () => {
9090

9191
expect(doc.id).toBeDefined()
9292
})
93+
94+
it('should not create duplicate versions with custom id type', async () => {
95+
const doc = await payload.create({
96+
collection: 'custom-ids',
97+
data: {
98+
title: 'hey',
99+
},
100+
})
101+
102+
await payload.update({
103+
collection: 'custom-ids',
104+
id: doc.id,
105+
data: {},
106+
})
107+
108+
await payload.update({
109+
collection: 'custom-ids',
110+
id: doc.id,
111+
data: {},
112+
})
113+
114+
const versionsQuery = await payload.db.findVersions({
115+
collection: 'custom-ids',
116+
req: {} as PayloadRequest,
117+
where: {
118+
'version.title': {
119+
equals: 'hey',
120+
},
121+
latest: {
122+
equals: true,
123+
},
124+
},
125+
})
126+
127+
expect(versionsQuery.totalDocs).toStrictEqual(1)
128+
})
129+
130+
it('should not accidentally treat nested id fields as custom id', () => {
131+
expect(payload.collections['fake-custom-ids'].customIDType).toBeUndefined()
132+
})
93133
})
94134

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

0 commit comments

Comments
 (0)