Repository: directus/directus
File: api/src/database/run-ast/utils/merge-with-parent-items.ts (TypeScript)

Code before:
				parentItem[nestedNode.fieldKey] = nestedItem;
			}
		}
	} else if (nestedNode.type === 'o2m') {
		const parentCollectionName = nestedNode.relation.related_collection;
		const parentPrimaryKeyField = schema.collections[parentCollectionName!]!.primary;
		const parentRelationField = nestedNode.fieldKey;
		const nestedParentKeyField = nestedNode.relation.field;

		const parentsByPrimaryKey = new Map();

		parentItems.forEach((parentItem: (typeof parentItems)[number]) => {
			if (!parentItem[parentRelationField]) parentItem[parentRelationField] = [];

			const parentPrimaryKey = parentItem[parentPrimaryKeyField];

			if (parentsByPrimaryKey.has(parentPrimaryKey)) {
				throw new Error(
					`Duplicate parent primary key '${parentPrimaryKey}' of '${parentCollectionName}' when merging o2m nested items`,
				);
			}

			parentsByPrimaryKey.set(parentPrimaryKey, parentItem);
		});

		const toAddToAllParents: typeof nestedItems = [];

		nestedItems.forEach((nestedItem) => {
			if (nestedItem === null) return;

			if (Array.isArray(nestedItem[nestedParentKeyField])) {
				toAddToAllParents.push(nestedItem); // TODO explain this odd case
				return; // Avoids adding the nestedItem twice
			}

			const parentPrimaryKey =
				nestedItem[nestedParentKeyField]?.[parentPrimaryKeyField] ?? nestedItem[nestedParentKeyField];

			const parentItem = parentsByPrimaryKey.get(parentPrimaryKey);

			if (!parentItem) {
				throw new Error(
					`Missing parentItem '${nestedItem[nestedParentKeyField]}' of '${parentCollectionName}' when merging o2m nested items`,
				);
			}

			parentItem[parentRelationField].push(nestedItem);
		});

		for (const [index, parentItem] of parentItems.entries()) {
			if (fieldAllowed === false || (isArray(fieldAllowed) && !fieldAllowed[index])) {

Review comment (suggestion):
```suggestion
		const toAddToAllParents: Item[] = [];
```

Code after:
				parentItem[nestedNode.fieldKey] = nestedItem;
			}
		}
	} else if (nestedNode.type === 'o2m') {
		const parentCollectionName = nestedNode.relation.related_collection;
		const parentPrimaryKeyField = schema.collections[parentCollectionName!]!.primary;
		const parentRelationField = nestedNode.fieldKey;
		const nestedParentKeyField = nestedNode.relation.field;

		const parentsByPrimaryKey = new Map<PrimaryKey, Item>();

		parentItems.forEach((parentItem: Item) => {
			if (!parentItem[parentRelationField]) parentItem[parentRelationField] = [];

			const parentPrimaryKey = parentItem[parentPrimaryKeyField];

			if (parentsByPrimaryKey.has(parentPrimaryKey)) {
				throw new Error(
					`Duplicate parent primary key '${parentPrimaryKey}' of '${parentCollectionName}' when merging o2m nested items`,
				);
			}

			parentsByPrimaryKey.set(parentPrimaryKey, parentItem);
		});

		const toAddToAllParents: Item[] = [];

		nestedItems.forEach((nestedItem) => {
			if (nestedItem === null) return;

			if (Array.isArray(nestedItem[nestedParentKeyField])) {
				toAddToAllParents.push(nestedItem); // TODO explain this odd case
				return; // Avoids adding the nestedItem twice
			}

			const parentPrimaryKey =
				nestedItem[nestedParentKeyField]?.[parentPrimaryKeyField] ?? nestedItem[nestedParentKeyField];

			const parentItem = parentsByPrimaryKey.get(parentPrimaryKey);

			if (!parentItem) {
				throw new Error(
					`Missing parentItem '${nestedItem[nestedParentKeyField]}' of '${parentCollectionName}' when merging o2m nested items`,
				);
			}

			parentItem[parentRelationField].push(nestedItem);
		});

		for (const [index, parentItem] of parentItems.entries()) {
			if (fieldAllowed === false || (isArray(fieldAllowed) && !fieldAllowed[index])) {