Skip to content

Commit 4030e21

Browse files
fix: allow self referencing relationships when adding new collections 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.
1 parent 646a534 commit 4030e21

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

packages/payload/src/collections/config/sanitize.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ export const sanitizeCollection = async (
3535
// Sanitize fields
3636
// /////////////////////////////////
3737

38-
const validRelationships = config.collections.map((c) => c.slug) || []
38+
const validRelationships = config.collections.reduce(
39+
(acc, c) => {
40+
acc.push(c.slug)
41+
return acc
42+
},
43+
[collection.slug],
44+
)
3945
const joins: SanitizedJoins = {}
4046
sanitized.fields = await sanitizeFields({
4147
collectionConfig: sanitized,

0 commit comments

Comments
 (0)