diff --git a/composer.lock b/composer.lock index 3ab0753d4..fd54a496d 100644 --- a/composer.lock +++ b/composer.lock @@ -839,16 +839,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.35", + "version": "1.10.36", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "e730e5facb75ffe09dfb229795e8c01a459f26c3" + "reference": "ffa3089511121a672e62969404e4fddc753f9b15" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/e730e5facb75ffe09dfb229795e8c01a459f26c3", - "reference": "e730e5facb75ffe09dfb229795e8c01a459f26c3", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/ffa3089511121a672e62969404e4fddc753f9b15", + "reference": "ffa3089511121a672e62969404e4fddc753f9b15", "shasum": "" }, "require": { @@ -897,7 +897,7 @@ "type": "tidelift" } ], - "time": "2023-09-19T15:27:56+00:00" + "time": "2023-09-29T14:07:45+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Database/Database.php b/src/Database/Database.php index 8292bf6bf..4c9c95793 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1537,11 +1537,15 @@ public function createRelationship( throw new DuplicateException('Attribute already exists'); } - if ($attribute->getAttribute('type') === self::VAR_RELATIONSHIP - && \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey) - && $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId() - ) { - throw new DuplicateException('Related attribute already exists'); + try { + if ($attribute->getAttribute('type') === self::VAR_RELATIONSHIP + && \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey) + && $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId() + ) { + throw new DuplicateException('Related attribute already exists'); + } + } catch (DuplicateException $e) { + $twoWayKey ??= $collection->getId() . '_' . uniqid(); } } diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 34609d113..a8b1c4af1 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -3429,6 +3429,47 @@ public function testNoChangeUpdateDocumentWithoutPermission(Document $document): return $document; } + public function testRelationSametwoWayKey(): void + { + if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + $this->expectNotToPerformAssertions(); + return; + } + + $permissions = [ + Permission::read(Role::any()), + Permission::create(Role::any()), + Permission::delete(Role::any()), + ]; + + static::getDatabase()->createCollection('c1', [], [], $permissions); + static::getDatabase()->createCollection('c2', [], [], $permissions); + + $res = static::getDatabase()->createRelationship( + collection: 'c1', + relatedCollection: 'c2', + type: Database::RELATION_ONE_TO_ONE, + id: 'c2' + ); + $this->assertTrue($res); + + $res = static::getDatabase()->createRelationship( + collection: 'c1', + relatedCollection: 'c2', + type: Database::RELATION_ONE_TO_MANY, + id: 'c1' + ); + $this->assertTrue($res); + + $res = static::getDatabase()->createRelationship( + collection: 'c1', + relatedCollection: 'c2', + type: Database::RELATION_MANY_TO_ONE, + id: 'c3' + ); + $this->assertTrue($res); + } + public function testNoChangeUpdateDocumentWithRelationWithoutPermission(): void { if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { @@ -5585,18 +5626,6 @@ public function testIdenticalTwoWayKeyRelationship(): void id: 'child1' ); - try { - static::getDatabase()->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()); - } - static::getDatabase()->createRelationship( collection: 'parent', relatedCollection: 'child',