From d63786e005d8f6b8609c6aae7f45957e52f5882e Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 24 Jun 2026 12:14:26 +0100 Subject: [PATCH] fix: purge collection cache after commit in createRelationship The two updateDocument() metadata writes run inside createRelationship's outer withTransaction(). Because the adapter nests transactions with a counter and only commits at the outermost level, each updateDocument()'s own post-commit purge fires while the outer transaction is still open (pre-commit). A concurrent reader can then re-cache the pre-commit collection, missing the new relationship column, and the subsequent createIndex() re-read validates against that stale copy and fails with 'Invalid index attribute "..." not found' (flaky, shared-tables/concurrent only). Purge the collection caches again after the outer commit, before creating the relationship indexes, so the re-read sees the committed schema. --- src/Database/Database.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Database/Database.php b/src/Database/Database.php index 74e75d883..91085fd94 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -3813,6 +3813,14 @@ public function createRelationship( throw new DatabaseException('Failed to create relationship: ' . $e->getMessage()); } + // updateDocument's purge ran inside this outer transaction (nested commits don't + // commit), so it fired pre-commit and a stale collection can stay cached. Re-purge + // after the real commit, else createIndex below re-reads it without the new column. + $this->withRetries(fn () => $this->purgeCachedCollection($collection->getId())); + $this->withRetries(fn () => $this->purgeCachedDocumentInternal(self::METADATA, $collection->getId())); + $this->withRetries(fn () => $this->purgeCachedCollection($relatedCollection->getId())); + $this->withRetries(fn () => $this->purgeCachedDocumentInternal(self::METADATA, $relatedCollection->getId())); + $indexKey = '_index_' . $id; $twoWayIndexKey = '_index_' . $twoWayKey; $indexesCreated = [];