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..beb4a982c 100644 --- a/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php +++ b/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php @@ -1024,26 +1024,37 @@ public function testIdenticalTwoWayKeyRelationship(): void id: 'child1' ); + $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); + try { $database->createRelationship( collection: 'parent', relatedCollection: 'child', type: Database::RELATION_ONE_TO_MANY, - id: 'children', + twoWay: true, + id: 'twoWayChildren', + twoWayKey: 'parent_id' ); $this->fail('Failed to throw Exception'); } catch (Exception $e) { $this->assertEquals('Related attribute already exists', $e->getMessage()); } - $database->createRelationship( - collection: 'parent', - relatedCollection: 'child', - type: Database::RELATION_ONE_TO_MANY, - id: 'children', - twoWayKey: 'parent_id' - ); - $collection = $database->getCollection('parent'); $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { @@ -1052,35 +1063,14 @@ 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']); } } - $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',