From 95552031eff2082d0bfb5e10bd3625eaca81deca Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 2 Apr 2026 03:01:58 +1300 Subject: [PATCH] (fix): register related collection ID mappings and update migration lib - getDatabasesDB now registers mappings for related collections from relationship attributes, not just the primary collection. This fixes testOneToOneRelationship where nested documents showed internal names. - Update utopia-php/migration to handle Index objects in createCollection. Co-Authored-By: Claude Opus 4.6 (1M context) --- app/init/resources.php | 30 +++++++++++++++++++++++++++++- composer.lock | 8 ++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/app/init/resources.php b/app/init/resources.php index 75fe4cfd79..8d4dc21df8 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -776,12 +776,40 @@ Http::setResource('getDatabasesDB', function (Group $pools, Cache $cache, Docume // Register collection ID mapping if collection document is provided if ($collection !== null && !$collection->isEmpty()) { + $dbSeq = $originalDatabase->getSequence(); + $dbPrefix = 'database_' . $dbSeq; $seq = $collection->getSequence(); if ($seq !== null) { - $dbPrefix = 'database_' . $originalDatabase->getSequence(); $metadata->setCollectionId('collection_' . $seq, $collection->getId()); $metadata->setCollectionId($dbPrefix . '_collection_' . $seq, $collection->getId()); } + + // Also register related collections from relationship attributes + $attributes = $collection->getAttribute('attributes', []); + foreach ($attributes as $attr) { + if ($attr->getAttribute('type') !== \Utopia\Query\Schema\ColumnType::Relationship->value) { + continue; + } + $options = $attr->getAttribute('options', []); + $relatedId = \is_array($options) ? ($options['relatedCollection'] ?? null) : null; + $relatedId ??= $attr->getAttribute('relatedCollection'); + if ($relatedId === null) { + continue; + } + // Look up the related collection from dbForProject + $relatedCol = $authorization->skip( + fn () => $dbForProject->silent( + fn () => $dbForProject->getDocument($dbPrefix, $relatedId) + ) + ); + if (!$relatedCol->isEmpty()) { + $relSeq = $relatedCol->getSequence(); + if ($relSeq !== null) { + $metadata->setCollectionId('collection_' . $relSeq, $relatedCol->getId()); + $metadata->setCollectionId($dbPrefix . '_collection_' . $relSeq, $relatedCol->getId()); + } + } + } } $database diff --git a/composer.lock b/composer.lock index 1432c20e98..38d25f7028 100644 --- a/composer.lock +++ b/composer.lock @@ -4708,12 +4708,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "7c0e2d4f065c72f8b8424bbc06cb4aecbf28915a" + "reference": "1bc59979ee7a1af5f98333f277cad27785ef30c1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/7c0e2d4f065c72f8b8424bbc06cb4aecbf28915a", - "reference": "7c0e2d4f065c72f8b8424bbc06cb4aecbf28915a", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/1bc59979ee7a1af5f98333f277cad27785ef30c1", + "reference": "1bc59979ee7a1af5f98333f277cad27785ef30c1", "shasum": "" }, "require": { @@ -4773,7 +4773,7 @@ "source": "https://github.com/utopia-php/migration/tree/feat-query-lib", "issues": "https://github.com/utopia-php/migration/issues" }, - "time": "2026-03-31T11:14:26+00:00" + "time": "2026-04-01T14:01:14+00:00" }, { "name": "utopia-php/mongo",