Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
) {
Expand Down
27 changes: 14 additions & 13 deletions tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', []);
Expand All @@ -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']);
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
}
Expand Down
Loading