Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions packages/drizzle/src/queries/getTableColumnFromPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,14 @@ export const getTableColumnFromPath = ({
}

if (!existingTable) {
const rootIDColumn =
adapter.versionsSuffix && rootTableName.endsWith(adapter.versionsSuffix)
? adapter.tables[rootTableName].parent
: adapter.tables[rootTableName].id
addJoinTable({
condition: and(
eq(
adapter.tables[rootTableName].id,
rootIDColumn,
aliasRelationshipTable[
`${(relationshipField.field as RelationshipField).relationTo as string}ID`
],
Expand Down Expand Up @@ -490,18 +494,20 @@ export const getTableColumnFromPath = ({
? adapter.tableNameMap.get(`${newTableName}_${toSnakeCase(onSegments[0])}`)
: undefined

const sourceIDColumn = aliasTable
? aliasTable.id
: adapter.versionsSuffix && tableName.endsWith(adapter.versionsSuffix)
? adapter.tables[tableName].parent
: adapter.tables[tableName].id

if (arrayTableName) {
// join from main table to array table
const { newAliasTable: arrayAliasTable } = getTableAlias({
adapter,
tableName: arrayTableName,
})

joins.push({
condition: eq(
arrayAliasTable[onSegments.slice(1).join('_')],
aliasTable ? aliasTable.id : adapter.tables[tableName].id,
),
condition: eq(arrayAliasTable[onSegments.slice(1).join('_')], sourceIDColumn),
queryPath: `${constraintPath}${field.name}._array`,
table: arrayAliasTable,
})
Expand All @@ -516,10 +522,7 @@ export const getTableColumnFromPath = ({
})
} else {
joins.push({
condition: eq(
newAliasTable[field.on.replaceAll('.', '_')],
aliasTable ? aliasTable.id : adapter.tables[tableName].id,
),
condition: eq(newAliasTable[field.on.replaceAll('.', '_')], sourceIDColumn),
queryPath: `${constraintPath}${field.name}`,
table: newAliasTable,
})
Expand Down
40 changes: 40 additions & 0 deletions test/joins/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
postsSlug,
restrictedCategoriesSlug,
restrictedPostsSlug,
textIdCategoriesVersionsSlug,
textIdPostsSlug,
} from './shared.js'

const filename = fileURLToPath(import.meta.url)
Expand Down Expand Up @@ -340,6 +342,44 @@ export default buildConfigWithDefaults({
},
FolderPoly1,
FolderPoly2,
{
slug: textIdCategoriesVersionsSlug,
versions: { drafts: true },
fields: [
{ name: 'id', type: 'text' },
{ name: 'name', type: 'text' },
{
name: 'relatedSingle',
type: 'join',
collection: textIdPostsSlug,
on: 'category',
},
{
name: 'relatedMany',
type: 'join',
collection: textIdPostsSlug,
on: 'categories',
},
],
},
{
slug: textIdPostsSlug,
fields: [
{ name: 'id', type: 'text' },
{ name: 'title', type: 'text' },
{
name: 'category',
type: 'relationship',
relationTo: textIdCategoriesVersionsSlug,
},
{
name: 'categories',
type: 'relationship',
relationTo: textIdCategoriesVersionsSlug,
hasMany: true,
},
],
},
],
localization: {
locales: [
Expand Down
34 changes: 34 additions & 0 deletions test/joins/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,40 @@ describe('Joins Field', () => {

expect(res.docs[0].relatedVersionsMany.docs[0].id).toBe(version.id)
})

it('should not crash when sorting drafts by a join field on a versioned collection with custom text id', async () => {
const category = await payload.create({
collection: 'text-id-categories-versions' as never,
data: { id: 'cat-text-1', name: 'cat-1' } as never,
})

await payload.create({
collection: 'text-id-posts' as never,
data: {
id: 'post-text-1',
title: 'post-1',
category: (category as { id: string }).id,
categories: [(category as { id: string }).id],
} as never,
})

const single = await payload.find({
collection: 'text-id-categories-versions' as never,
draft: true,
sort: 'relatedSingle',
})
expect(single.docs).toHaveLength(1)

const many = await payload.find({
collection: 'text-id-categories-versions' as never,
draft: true,
sort: 'relatedMany',
})
expect(many.docs).toHaveLength(1)

await payload.delete({ collection: 'text-id-posts' as never, where: {} })
await payload.delete({ collection: 'text-id-categories-versions' as never, where: {} })
})
})

describe('REST', () => {
Expand Down
4 changes: 4 additions & 0 deletions test/joins/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export const categoriesVersionsSlug = 'categories-versions'

export const versionsSlug = 'versions'

export const textIdCategoriesVersionsSlug = 'text-id-categories-versions'

export const textIdPostsSlug = 'text-id-posts'

export const collectionSlugs = [
categoriesSlug,
categoriesVersionsSlug,
Expand Down
Loading