fix(db-postgres): querying on hasMany: true select field in a relationship (#12916)
Fixes https://github.com/payloadcms/payload/issues/11635
This commit is contained in:
@@ -799,9 +799,10 @@ export const getTableColumnFromPath = ({
|
||||
`${tableName}_${tableNameSuffix}${toSnakeCase(field.name)}`,
|
||||
)
|
||||
|
||||
const idColumn = (aliasTable ?? adapter.tables[tableName]).id
|
||||
if (locale && isFieldLocalized && adapter.payload.config.localization) {
|
||||
const conditions = [
|
||||
eq(adapter.tables[tableName].id, adapter.tables[newTableName].parent),
|
||||
eq(idColumn, adapter.tables[newTableName].parent),
|
||||
eq(adapter.tables[newTableName]._locale, locale),
|
||||
]
|
||||
|
||||
@@ -816,7 +817,7 @@ export const getTableColumnFromPath = ({
|
||||
})
|
||||
} else {
|
||||
addJoinTable({
|
||||
condition: eq(adapter.tables[tableName].id, adapter.tables[newTableName].parent),
|
||||
condition: eq(idColumn, adapter.tables[newTableName].parent),
|
||||
joins,
|
||||
table: adapter.tables[newTableName],
|
||||
})
|
||||
|
||||
@@ -229,6 +229,12 @@ export default buildConfigWithDefaults({
|
||||
name: 'name',
|
||||
type: 'text',
|
||||
},
|
||||
{
|
||||
name: 'select',
|
||||
type: 'select',
|
||||
hasMany: true,
|
||||
options: ['a', 'b', 'c'],
|
||||
},
|
||||
{
|
||||
name: 'director',
|
||||
type: 'relationship',
|
||||
|
||||
@@ -429,6 +429,21 @@ describe('Relationships', () => {
|
||||
expect(result.docs[0].id).toBe(id)
|
||||
})
|
||||
|
||||
it('should allow query hasMany select in relationship', async () => {
|
||||
const movie = await payload.create({ collection: 'movies', data: { select: ['a', 'b'] } })
|
||||
const doc = await payload.create({
|
||||
collection: 'directors',
|
||||
data: { name: 'Mega Director', movie },
|
||||
})
|
||||
|
||||
const res = await payload.find({
|
||||
collection: 'directors',
|
||||
where: { 'movie.select': { equals: 'a' } },
|
||||
})
|
||||
expect(res.docs).toHaveLength(1)
|
||||
expect(res.docs[0].id).toBe(doc.id)
|
||||
})
|
||||
|
||||
it('should allow 4x deep querying', async () => {
|
||||
const movie_1 = await payload.create({
|
||||
collection: 'movies',
|
||||
|
||||
@@ -266,6 +266,7 @@ export interface Screening {
|
||||
export interface Movie {
|
||||
id: string;
|
||||
name?: string | null;
|
||||
select?: ('a' | 'b' | 'c')[] | null;
|
||||
director?: (string | null) | Director;
|
||||
updatedAt: string;
|
||||
createdAt: string;
|
||||
@@ -730,6 +731,7 @@ export interface ScreeningsSelect<T extends boolean = true> {
|
||||
*/
|
||||
export interface MoviesSelect<T extends boolean = true> {
|
||||
name?: T;
|
||||
select?: T;
|
||||
director?: T;
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
|
||||
Reference in New Issue
Block a user