diff --git a/src/Database/Database.php b/src/Database/Database.php index d581529fa5..a6a5cc2f8d 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -8194,6 +8194,10 @@ private function deleteSetNull(Document $collection, Document $relatedCollection if ($side === Database::RELATION_SIDE_CHILD) { break; } + + if (empty($value)) { + break; + } foreach ($value as $relation) { $this->authorization->skip(function () use ($relatedCollection, $twoWayKey, $relation) { $this->skipRelationships(fn () => $this->updateDocument( @@ -8212,12 +8216,14 @@ private function deleteSetNull(Document $collection, Document $relatedCollection break; } - if (!$twoWay) { - $value = $this->find($relatedCollection->getId(), [ - Query::select(['$id']), - Query::equal($twoWayKey, [$document->getId()]), - Query::limit(PHP_INT_MAX) - ]); + $value = $this->find($relatedCollection->getId(), [ + Query::select(['$id']), + Query::equal($twoWayKey, [$document->getId()]), + Query::limit(PHP_INT_MAX) + ]); + + if (empty($value)) { + break; } foreach ($value as $relation) { diff --git a/tests/e2e/Adapter/Scopes/Relationships/ManyToOneTests.php b/tests/e2e/Adapter/Scopes/Relationships/ManyToOneTests.php index e62ff735c3..0e81285959 100644 --- a/tests/e2e/Adapter/Scopes/Relationships/ManyToOneTests.php +++ b/tests/e2e/Adapter/Scopes/Relationships/ManyToOneTests.php @@ -782,6 +782,24 @@ public function testManyToOneTwoWayRelationship(): void $database->getDocument('product', 'product1'); $this->assertEquals(null, $product1->getAttribute('newStore')); + + // Create child with no related parents and verify deleteSetNull succeeds + $database->createDocument('store', new Document([ + '$id' => 'store8', + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + 'name' => 'Store 8', + 'opensAt' => '10:00', + ])); + + $deleted = $database->deleteDocument('store', 'store8'); + $this->assertEquals(true, $deleted); + + $store8 = $database->getDocument('store', 'store8'); + $this->assertEquals(true, $store8->isEmpty()); // Change on delete to cascade $database->updateRelationship( collection: 'product',