Skip to content

Commit

Permalink
fix: allow self referencing relationships when adding new collections…
Browse files Browse the repository at this point in the history
… to config (#9360)

### What?
Unable to add collections to the config dynamically if they reference
their own collection in a relationship field.

This was discovered while working on the folder view feature which
dynamically adds collections to your config if it is enabled per
collection.

### Why?
When `sanitizeCollection` runs, it takes the current config. If you are
sanitizing a collection before adding it to the config, that collection
cannot have any self referencing relationship fields on it otherwise it
fails the validRelationships check.

### How?
Using a reducer we now initialize the validRelationships variable with
the incoming collection slug.
  • Loading branch information
JarrodMFlesch authored Nov 19, 2024
1 parent 646a534 commit 4030e21
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/payload/src/collections/config/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ export const sanitizeCollection = async (
// Sanitize fields
// /////////////////////////////////

const validRelationships = config.collections.map((c) => c.slug) || []
const validRelationships = config.collections.reduce(
(acc, c) => {
acc.push(c.slug)
return acc
},
[collection.slug],
)
const joins: SanitizedJoins = {}
sanitized.fields = await sanitizeFields({
collectionConfig: sanitized,
Expand Down

0 comments on commit 4030e21

Please sign in to comment.