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',
|
type: 'many',
|
||||||
// joins are not localized on the parent table
|
// joins are not localized on the parent table
|
||||||
localized: false,
|
localized: false,
|
||||||
relationName: toSnakeCase(field.on),
|
relationName: field.on.replaceAll('.', '_'),
|
||||||
target,
|
target,
|
||||||
})
|
})
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -915,7 +915,7 @@ export const traverseFields = ({
|
|||||||
type: 'many',
|
type: 'many',
|
||||||
// joins are not localized on the parent table
|
// joins are not localized on the parent table
|
||||||
localized: false,
|
localized: false,
|
||||||
relationName: toSnakeCase(field.on),
|
relationName: field.on.replaceAll('.', '_'),
|
||||||
target,
|
target,
|
||||||
})
|
})
|
||||||
break
|
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,
|
collection: postsSlug,
|
||||||
on: 'group.category',
|
on: 'group.category',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'camelCasePosts',
|
||||||
|
type: 'join',
|
||||||
|
collection: postsSlug,
|
||||||
|
on: 'group.camelCaseCategory',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -32,6 +32,11 @@ export const Posts: CollectionConfig = {
|
|||||||
type: 'relationship',
|
type: 'relationship',
|
||||||
relationTo: categoriesSlug,
|
relationTo: categoriesSlug,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'camelCaseCategory',
|
||||||
|
type: 'relationship',
|
||||||
|
relationTo: categoriesSlug,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ describe('Joins Field', () => {
|
|||||||
upload: uploadedImage,
|
upload: uploadedImage,
|
||||||
group: {
|
group: {
|
||||||
category: category.id,
|
category: category.id,
|
||||||
|
camelCaseCategory: category.id,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -107,6 +108,17 @@ describe('Joins Field', () => {
|
|||||||
expect(docs[0].category.relatedPosts.docs).toHaveLength(10)
|
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 () => {
|
it('should populate uploads in joins', async () => {
|
||||||
const { docs } = await payload.find({
|
const { docs } = await payload.find({
|
||||||
limit: 1,
|
limit: 1,
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ export interface Post {
|
|||||||
category?: (string | null) | Category;
|
category?: (string | null) | Category;
|
||||||
group?: {
|
group?: {
|
||||||
category?: (string | null) | Category;
|
category?: (string | null) | Category;
|
||||||
|
camelCaseCategory?: (string | null) | Category;
|
||||||
};
|
};
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
@@ -101,6 +102,10 @@ export interface Category {
|
|||||||
docs?: (string | Post)[] | null;
|
docs?: (string | Post)[] | null;
|
||||||
hasNextPage?: boolean | null;
|
hasNextPage?: boolean | null;
|
||||||
} | null;
|
} | null;
|
||||||
|
camelCasePosts?: {
|
||||||
|
docs?: (string | Post)[] | null;
|
||||||
|
hasNextPage?: boolean | null;
|
||||||
|
} | null;
|
||||||
};
|
};
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user