fix(db-postgres): querying on array wtihin a relationship field (#8152)

## Description

Fixes https://github.com/payloadcms/payload/issues/6037

- [x] I have read and understand the
[CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md)
document in this repository.

## Type of change

<!-- Please delete options that are not relevant. -->

- [x] Bug fix (non-breaking change which fixes an issue)

## Checklist:

- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] Existing test suite passes locally with my changes
This commit is contained in:
Sasha
2024-09-10 15:44:38 +03:00
committed by GitHub
parent df023a52fd
commit 0c563ebd73
5 changed files with 90 additions and 4 deletions

View File

@@ -297,11 +297,13 @@ export const getTableColumnFromPath = ({
`${tableName}_${tableNameSuffix}${toSnakeCase(field.name)}`,
)
const arrayParentTable = aliasTable || adapter.tables[tableName]
constraintPath = `${constraintPath}${field.name}.%.`
if (locale && field.localized && adapter.payload.config.localization) {
joins.push({
condition: and(
eq(adapter.tables[tableName].id, adapter.tables[newTableName]._parentID),
eq(arrayParentTable.id, adapter.tables[newTableName]._parentID),
eq(adapter.tables[newTableName]._locale, locale),
),
table: adapter.tables[newTableName],
@@ -315,7 +317,7 @@ export const getTableColumnFromPath = ({
}
} else {
joins.push({
condition: eq(adapter.tables[tableName].id, adapter.tables[newTableName]._parentID),
condition: eq(arrayParentTable.id, adapter.tables[newTableName]._parentID),
table: adapter.tables[newTableName],
})
}

View File

@@ -54,8 +54,8 @@ export interface UserAuthOperations {
*/
export interface Post {
id: string;
customClientField?: string | null;
customServerField?: string | null;
text?: string | null;
serverTextField?: string | null;
richText?: {
root: {
type: string;

View File

@@ -290,6 +290,31 @@ export default buildConfigWithDefaults({
},
],
},
{
slug: 'pages',
fields: [
{
type: 'array',
name: 'menu',
fields: [
{
name: 'label',
type: 'text',
},
],
},
],
},
{
slug: 'rels-to-pages',
fields: [
{
name: 'page',
type: 'relationship',
relationTo: 'pages',
},
],
},
],
onInit: async (payload) => {
await payload.create({

View File

@@ -10,6 +10,7 @@ import type {
CustomIdNumberRelation,
CustomIdRelation,
Director,
Page,
Post,
PostsLocalized,
Relation,
@@ -676,6 +677,37 @@ describe('Relationships', () => {
expect(query.docs).toHaveLength(1)
expect(query.docs[0].id).toStrictEqual(firstLevelID)
})
it('should allow querying within array nesting', async () => {
const page = await payload.create({
collection: 'pages',
data: {
menu: [
{
label: 'hello',
},
],
},
})
const rel = await payload.create({ collection: 'rels-to-pages', data: { page: page.id } })
const resEquals = await payload.find({
collection: 'rels-to-pages',
where: { 'page.menu.label': { equals: 'hello' } },
})
expect(resEquals.totalDocs).toBe(1)
expect(resEquals.docs[0].id).toBe(rel.id)
const resIn = await payload.find({
collection: 'rels-to-pages',
where: { 'page.menu.label': { in: ['hello'] } },
})
expect(resIn.totalDocs).toBe(1)
expect(resIn.docs[0].id).toBe(rel.id)
})
})
describe('Nested Querying Separate Collections', () => {

View File

@@ -24,6 +24,8 @@ export interface Config {
movieReviews: MovieReview;
'polymorphic-relationships': PolymorphicRelationship;
tree: Tree;
pages: Page;
'rels-to-pages': RelsToPage;
users: User;
'payload-preferences': PayloadPreference;
'payload-migrations': PayloadMigration;
@@ -224,6 +226,31 @@ export interface Tree {
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "pages".
*/
export interface Page {
id: string;
menu?:
| {
label?: string | null;
id?: string | null;
}[]
| null;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "rels-to-pages".
*/
export interface RelsToPage {
id: string;
page?: (string | null) | Page;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-preferences".