diff --git a/src/Database/Database.php b/src/Database/Database.php index 4ee1725db..f6bd3cdec 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4038,6 +4038,9 @@ private function deleteSetNull(Document $collection, Document $relatedCollection Query::equal($twoWayKey, [$document->getId()]) ]); } else { + if(empty($value)) { + return; + } $related = $this->getDocument($relatedCollection->getId(), $value->getId()); } diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 31b0dc9df..680f88112 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -5571,6 +5571,15 @@ public function testOneToOneTwoWayRelationship(): void onDelete: Database::RELATION_MUTATE_SET_NULL ); + static::getDatabase()->updateDocument('city', 'city1', new Document(['newCountry' => null, '$id' => 'city1'])); + $city1 = static::getDatabase()->getDocument('city', 'city1'); + $this->assertNull($city1->getAttribute('newCountry')); + + // Check Delete TwoWay TRUE && RELATION_MUTATE_SET_NULL && related value NULL + $this->assertTrue(static::getDatabase()->deleteDocument('city', 'city1')); + $city1 = static::getDatabase()->getDocument('city', 'city1'); + $this->assertTrue($city1->isEmpty()); + // Delete parent, will set child relationship to null for two-way static::getDatabase()->deleteDocument('country', 'country1');