Skip to content

Commit 27b1629

Browse files
authored
fix(db-postgres, db-sqlite): joins relation name (#8491)
A join field on a relationship with a camelCase name would cause an error from drizzle due to the relation name not matching.
1 parent dfdea0d commit 27b1629

File tree

7 files changed

+31
-3
lines changed

7 files changed

+31
-3
lines changed

packages/db-sqlite/src/schema/traverseFields.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ export const traverseFields = ({
907907
type: 'many',
908908
// joins are not localized on the parent table
909909
localized: false,
910-
relationName: toSnakeCase(field.on),
910+
relationName: field.on.replaceAll('.', '_'),
911911
target,
912912
})
913913
break

packages/drizzle/src/postgres/schema/traverseFields.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ export const traverseFields = ({
915915
type: 'many',
916916
// joins are not localized on the parent table
917917
localized: false,
918-
relationName: toSnakeCase(field.on),
918+
relationName: field.on.replaceAll('.', '_'),
919919
target,
920920
})
921921
break

test/joins/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/uploads/
1+
uploads

test/joins/collections/Categories.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ export const Categories: CollectionConfig = {
6060
collection: postsSlug,
6161
on: 'group.category',
6262
},
63+
{
64+
name: 'camelCasePosts',
65+
type: 'join',
66+
collection: postsSlug,
67+
on: 'group.camelCaseCategory',
68+
},
6369
],
6470
},
6571
],

test/joins/collections/Posts.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export const Posts: CollectionConfig = {
3232
type: 'relationship',
3333
relationTo: categoriesSlug,
3434
},
35+
{
36+
name: 'camelCaseCategory',
37+
type: 'relationship',
38+
relationTo: categoriesSlug,
39+
},
3540
],
3641
},
3742
],

test/joins/int.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ describe('Joins Field', () => {
6868
upload: uploadedImage,
6969
group: {
7070
category: category.id,
71+
camelCaseCategory: category.id,
7172
},
7273
})
7374
}
@@ -107,6 +108,17 @@ describe('Joins Field', () => {
107108
expect(docs[0].category.relatedPosts.docs).toHaveLength(10)
108109
})
109110

111+
it('should populate relationships in joins with camelCase names', async () => {
112+
const { docs } = await payload.find({
113+
limit: 1,
114+
collection: 'posts',
115+
})
116+
117+
expect(docs[0].group.camelCaseCategory.id).toBeDefined()
118+
expect(docs[0].group.camelCaseCategory.name).toBeDefined()
119+
expect(docs[0].group.camelCaseCategory.group.camelCasePosts.docs).toHaveLength(10)
120+
})
121+
110122
it('should populate uploads in joins', async () => {
111123
const { docs } = await payload.find({
112124
limit: 1,

test/joins/payload-types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export interface Post {
5959
category?: (string | null) | Category;
6060
group?: {
6161
category?: (string | null) | Category;
62+
camelCaseCategory?: (string | null) | Category;
6263
};
6364
updatedAt: string;
6465
createdAt: string;
@@ -101,6 +102,10 @@ export interface Category {
101102
docs?: (string | Post)[] | null;
102103
hasNextPage?: boolean | null;
103104
} | null;
105+
camelCasePosts?: {
106+
docs?: (string | Post)[] | null;
107+
hasNextPage?: boolean | null;
108+
} | null;
104109
};
105110
updatedAt: string;
106111
createdAt: string;

0 commit comments

Comments
 (0)