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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
2
test/joins/.gitignore
vendored
2
test/joins/.gitignore
vendored
@@ -1 +1 @@
|
||||
/uploads/
|
||||
uploads
|
||||
|
||||
@@ -60,6 +60,12 @@ export const Categories: CollectionConfig = {
|
||||
collection: postsSlug,
|
||||
on: 'group.category',
|
||||
},
|
||||
{
|
||||
name: 'camelCasePosts',
|
||||
type: 'join',
|
||||
collection: postsSlug,
|
||||
on: 'group.camelCaseCategory',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
|
||||
@@ -32,6 +32,11 @@ export const Posts: CollectionConfig = {
|
||||
type: 'relationship',
|
||||
relationTo: categoriesSlug,
|
||||
},
|
||||
{
|
||||
name: 'camelCaseCategory',
|
||||
type: 'relationship',
|
||||
relationTo: categoriesSlug,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user