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.
This commit is contained in:
Dan Ribbens
2024-09-30 16:47:21 -04:00
committed by GitHub
parent dfdea0d4eb
commit 27b1629927
7 changed files with 31 additions and 3 deletions

View File

@@ -907,7 +907,7 @@ export const traverseFields = ({
type: 'many',
// joins are not localized on the parent table
localized: false,
relationName: toSnakeCase(field.on),
relationName: field.on.replaceAll('.', '_'),
target,
})
break

View File

@@ -915,7 +915,7 @@ export const traverseFields = ({
type: 'many',
// joins are not localized on the parent table
localized: false,
relationName: toSnakeCase(field.on),
relationName: field.on.replaceAll('.', '_'),
target,
})
break

View File

@@ -1 +1 @@
/uploads/
uploads

View File

@@ -60,6 +60,12 @@ export const Categories: CollectionConfig = {
collection: postsSlug,
on: 'group.category',
},
{
name: 'camelCasePosts',
type: 'join',
collection: postsSlug,
on: 'group.camelCaseCategory',
},
],
},
],

View File

@@ -32,6 +32,11 @@ export const Posts: CollectionConfig = {
type: 'relationship',
relationTo: categoriesSlug,
},
{
name: 'camelCaseCategory',
type: 'relationship',
relationTo: categoriesSlug,
},
],
},
],

View File

@@ -68,6 +68,7 @@ describe('Joins Field', () => {
upload: uploadedImage,
group: {
category: category.id,
camelCaseCategory: category.id,
},
})
}
@@ -107,6 +108,17 @@ describe('Joins Field', () => {
expect(docs[0].category.relatedPosts.docs).toHaveLength(10)
})
it('should populate relationships in joins with camelCase names', async () => {
const { docs } = await payload.find({
limit: 1,
collection: 'posts',
})
expect(docs[0].group.camelCaseCategory.id).toBeDefined()
expect(docs[0].group.camelCaseCategory.name).toBeDefined()
expect(docs[0].group.camelCaseCategory.group.camelCasePosts.docs).toHaveLength(10)
})
it('should populate uploads in joins', async () => {
const { docs } = await payload.find({
limit: 1,

View File

@@ -59,6 +59,7 @@ export interface Post {
category?: (string | null) | Category;
group?: {
category?: (string | null) | Category;
camelCaseCategory?: (string | null) | Category;
};
updatedAt: string;
createdAt: string;
@@ -101,6 +102,10 @@ export interface Category {
docs?: (string | Post)[] | null;
hasNextPage?: boolean | null;
} | null;
camelCasePosts?: {
docs?: (string | Post)[] | null;
hasNextPage?: boolean | null;
} | null;
};
updatedAt: string;
createdAt: string;