payload icon indicating copy to clipboard operation
payload copied to clipboard

fix(db-postgres): querying by nested to rows fields in has many relationships

Open r1tsuu opened this issue 2 months ago • 0 comments

What?

Querying by nested to rows fields in has many relationships like this:

const result = await payload.find({
  collection: 'relationship-fields',
  where: {
    'relationToRowMany.title': { equals: 'some-title' },
  },
})

Where the related collection:

const RowFields: CollectionConfig = {
  slug: rowFieldsSlug,
  fields: [
    {
      type: 'row',
      fields: [
        {
          name: 'title',
          label: 'Title within a row',
          type: 'text',
          required: true,
        },
      ],
    },
  ],
}

was broken

Why?

We migrated to use flattenedFields, but not in this specific case. This error would be caught earlier we used noImplictAny typescript rule. https://www.typescriptlang.org/tsconfig/#noImplicitAny which wouldn't allow us to create variable like this:

let relationshipFields // relationshipFields is any here

Instead, we should write:

let relationshipFields: FlattenedField[]

We should migrate to it and strictNullChecks as well.

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

r1tsuu avatar Dec 12 '24 21:12 r1tsuu