From 576ca46f467ade12df6a5a3b70e7decfba8c8150 Mon Sep 17 00:00:00 2001 From: Harsh Mahajan Date: Tue, 24 Mar 2026 12:56:52 +0530 Subject: [PATCH 1/3] fix --- src/Database/Database.php | 3 ++- .../Scopes/Relationships/OneToOneTests.php | 27 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index ac58d72f0..60c4db238 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -3589,7 +3589,8 @@ public function createRelationship( } if ( - $attribute->getAttribute('type') === self::VAR_RELATIONSHIP + $twoWay + && $attribute->getAttribute('type') === self::VAR_RELATIONSHIP && \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey) && $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId() ) { diff --git a/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php b/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php index e67c41138..fd6a49be9 100644 --- a/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php +++ b/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php @@ -1024,25 +1024,22 @@ public function testIdenticalTwoWayKeyRelationship(): void id: 'child1' ); - try { - $database->createRelationship( - collection: 'parent', - relatedCollection: 'child', - type: Database::RELATION_ONE_TO_MANY, - id: 'children', - ); - $this->fail('Failed to throw Exception'); - } catch (Exception $e) { - $this->assertEquals('Related attribute already exists', $e->getMessage()); - } - - $database->createRelationship( + $result = $database->createRelationship( collection: 'parent', relatedCollection: 'child', type: Database::RELATION_ONE_TO_MANY, id: 'children', + ); + $this->assertTrue($result); + + $result = $database->createRelationship( + collection: 'parent', + relatedCollection: 'child', + type: Database::RELATION_ONE_TO_MANY, + id: 'childrenById', twoWayKey: 'parent_id' ); + $this->assertTrue($result); $collection = $database->getCollection('parent'); $attributes = $collection->getAttribute('attributes', []); @@ -1052,6 +1049,10 @@ public function testIdenticalTwoWayKeyRelationship(): void } if ($attribute['key'] === 'children') { + $this->assertEquals('parent', $attribute['options']['twoWayKey']); + } + + if ($attribute['key'] === 'childrenById') { $this->assertEquals('parent_id', $attribute['options']['twoWayKey']); } } From aa4355ad0d18e35e2d2b0846e7ff2f7a5c424b27 Mon Sep 17 00:00:00 2001 From: Harsh Mahajan Date: Tue, 24 Mar 2026 13:08:08 +0530 Subject: [PATCH 2/3] remove test --- .../Scopes/Relationships/OneToOneTests.php | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php b/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php index fd6a49be9..ad5e53dd5 100644 --- a/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php +++ b/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php @@ -1057,31 +1057,6 @@ public function testIdenticalTwoWayKeyRelationship(): void } } - $database->createDocument('parent', new Document([ - '$permissions' => [ - Permission::read(Role::any()), - Permission::update(Role::any()), - Permission::delete(Role::any()), - ], - 'child1' => [ - '$id' => 'foo', - '$permissions' => [Permission::read(Role::any())], - ], - 'children' => [ - [ - '$id' => 'bar', - '$permissions' => [Permission::read(Role::any())], - ], - ], - ])); - - $documents = $database->find('parent', []); - $document = array_pop($documents); - $this->assertArrayHasKey('child1', $document); - $this->assertEquals('foo', $document->getAttribute('child1')->getId()); - $this->assertArrayHasKey('children', $document); - $this->assertEquals('bar', $document->getAttribute('children')[0]->getId()); - try { $database->updateRelationship( collection: 'parent', From 84ed291ddc4dd77715049c525969100f0dd20cab Mon Sep 17 00:00:00 2001 From: Harsh Mahajan Date: Tue, 24 Mar 2026 13:09:39 +0530 Subject: [PATCH 3/3] add test --- .../Adapter/Scopes/Relationships/OneToOneTests.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php b/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php index ad5e53dd5..beb4a982c 100644 --- a/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php +++ b/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php @@ -1041,6 +1041,20 @@ public function testIdenticalTwoWayKeyRelationship(): void ); $this->assertTrue($result); + try { + $database->createRelationship( + collection: 'parent', + relatedCollection: 'child', + type: Database::RELATION_ONE_TO_MANY, + twoWay: true, + id: 'twoWayChildren', + twoWayKey: 'parent_id' + ); + $this->fail('Failed to throw Exception'); + } catch (Exception $e) { + $this->assertEquals('Related attribute already exists', $e->getMessage()); + } + $collection = $database->getCollection('parent'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) {