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
7 changes: 4 additions & 3 deletions packages/drizzle/src/schema/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
fieldAffectsData,
fieldIsVirtual,
fieldShouldBeLocalized,
flattenAllFields,
optionIsObject,
} from 'payload/shared'
import toSnakeCase from 'to-snake-case'
Expand Down Expand Up @@ -947,9 +948,9 @@ export const traverseFields = ({

// get the id type of the related collection
let colType: IDType = isUUIDType(adapter.idType) ? 'uuid' : 'integer'
const relatedCollectionCustomID = relationshipConfig.fields.find(
(field) => fieldAffectsData(field) && field.name === 'id',
)
const relatedCollectionCustomID = flattenAllFields({
fields: relationshipConfig.fields,
}).find((field) => fieldAffectsData(field) && field.name === 'id')
if (relatedCollectionCustomID?.type === 'number') {
colType = 'numeric'
}
Expand Down
28 changes: 28 additions & 0 deletions test/relationships/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { devUser } from '../credentials.js'
import {
chainedRelSlug,
customIdNumberSlug,
customIdRowSlug,
customIdSlug,
defaultAccessRelSlug,
polymorphicRelationshipsSlug,
Expand Down Expand Up @@ -128,6 +129,11 @@ export default buildConfigWithDefaults({
type: 'relationship',
relationTo: customIdNumberSlug,
},
{
name: 'customIdRowRelation',
type: 'relationship',
relationTo: customIdRowSlug,
},
{
name: 'filteredRelation',
type: 'relationship',
Expand Down Expand Up @@ -214,6 +220,28 @@ export default buildConfigWithDefaults({
],
versions: false,
},
{
// Regression test: custom text id nested inside a row layout field.
// The Drizzle schema builder must find the id via flattened fields,
// otherwise FK columns default to integer and startup crashes.
slug: customIdRowSlug,
fields: [
{
type: 'row',
fields: [
{
name: 'id',
type: 'text',
},
{
name: 'name',
type: 'text',
},
],
},
],
versions: false,
},
{
slug: 'screenings',
fields: [
Expand Down
14 changes: 14 additions & 0 deletions test/relationships/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { mongooseList } from '../__helpers/shared/isMongoose.js'
import {
chainedRelSlug,
customIdNumberSlug,
customIdRowSlug,
customIdSlug,
defaultAccessRelSlug,
polymorphicRelationshipsSlug,
Expand Down Expand Up @@ -960,6 +961,19 @@ describe('Relationships', () => {
.then((res) => res.json())
expect(customIdNumberRelation).toMatchObject({ id: generatedCustomIdNumber })
})

it('should resolve FK type correctly when custom text id is nested in a row field', async () => {
// Regression: traverseFields used a shallow find on collection.fields.
// A custom text id inside a row/group/collapsible was not detected,
// causing FK columns to be integer instead of varchar → startup crash.
const rowDoc = await payload.create({
collection: customIdRowSlug,
data: { id: 'row-custom-id', name: 'Row Custom ID' },
})
expect(rowDoc.id).toBe('row-custom-id')

await payload.delete({ collection: customIdRowSlug, id: rowDoc.id })
})
})

describe('depth', () => {
Expand Down
1 change: 1 addition & 0 deletions test/relationships/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export const defaultAccessRelSlug = 'strict-access'
export const chainedRelSlug = 'chained'
export const customIdSlug = 'custom-id'
export const customIdNumberSlug = 'custom-id-number'
export const customIdRowSlug = 'custom-id-row'
export const polymorphicRelationshipsSlug = 'polymorphic-relationships'
export const treeSlug = 'tree'
Loading