diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Relationship/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Relationship/Create.php index fdd40aaa8f..20fca8c16c 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Relationship/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Relationship/Create.php @@ -93,8 +93,8 @@ class Create extends Action throw new Exception(Exception::GENERAL_FEATURE_UNSUPPORTED, 'Relationships are not supported by this database.'); } - $key ??= $relatedCollectionId; $twoWayKeyWasProvided = $twoWayKey !== null; + $key ??= $relatedCollectionId; $twoWayKey ??= $collectionId; $database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId)); @@ -114,6 +114,10 @@ class Create extends Action throw new Exception($this->getParentNotFoundException(), params: [$relatedCollectionId]); } + if (!$twoWay) { + $twoWayKey = ''; + } + $attributes = $collection->getAttribute('attributes', []); foreach ($attributes as $attribute) { if ($attribute->getAttribute('type') !== Database::VAR_RELATIONSHIP) { @@ -125,6 +129,7 @@ class Create extends Action } if ( + $twoWay && \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey) && $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId() ) { diff --git a/tests/e2e/Services/Databases/DatabasesBase.php b/tests/e2e/Services/Databases/DatabasesBase.php index 7f23f2966c..066d0fadd5 100644 --- a/tests/e2e/Services/Databases/DatabasesBase.php +++ b/tests/e2e/Services/Databases/DatabasesBase.php @@ -6447,6 +6447,183 @@ trait DatabasesBase $this->assertEquals(Database::RELATION_MUTATE_CASCADE, $attribute['body']['onDelete']); } + public function testOneWayRelationshipDoesNotRequireTwoWayKey(): void + { + if (!$this->getSupportForRelationships()) { + $this->expectNotToPerformAssertions(); + return; + } + + $data = $this->setupDatabase(); + $databaseId = $data['databaseId']; + + $source = $this->client->call(Client::METHOD_POST, $this->getContainerUrl($databaseId), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getContainerIdParam() => ID::unique(), + 'name' => 'Source', + $this->getSecurityParam() => true, + ]); + + $related = $this->client->call(Client::METHOD_POST, $this->getContainerUrl($databaseId), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getContainerIdParam() => ID::unique(), + 'name' => 'Related', + $this->getSecurityParam() => true, + ]); + + $existing = $this->client->call(Client::METHOD_POST, $this->getSchemaUrl($databaseId, $source['body']['$id']) . '/relationship', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getRelatedIdParam() => $related['body']['$id'], + 'type' => Database::RELATION_ONE_TO_ONE, + 'twoWay' => true, + 'key' => 'existingRelation', + 'twoWayKey' => $source['body']['$id'], + 'onDelete' => Database::RELATION_MUTATE_CASCADE, + ]); + + $this->assertEquals(202, $existing['headers']['status-code']); + $this->waitForAttribute($databaseId, $source['body']['$id'], 'existingRelation'); + $this->waitForAttribute($databaseId, $related['body']['$id'], $source['body']['$id']); + + $response = $this->client->call(Client::METHOD_POST, $this->getSchemaUrl($databaseId, $source['body']['$id']) . '/relationship', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getRelatedIdParam() => $related['body']['$id'], + 'type' => Database::RELATION_ONE_TO_ONE, + 'twoWay' => false, + 'key' => 'myRelation', + 'onDelete' => Database::RELATION_MUTATE_CASCADE, + ]); + + $this->assertEquals(202, $response['headers']['status-code'], json_encode($response['body'])); + $this->assertEquals('myRelation', $response['body']['key']); + $this->assertEquals(false, $response['body']['twoWay']); + $this->assertEquals('', $response['body']['twoWayKey']); + + $this->waitForAttribute($databaseId, $source['body']['$id'], 'myRelation'); + } + + public function testOneWayRelationshipIgnoresProvidedTwoWayKey(): void + { + if (!$this->getSupportForRelationships()) { + $this->expectNotToPerformAssertions(); + return; + } + + $data = $this->setupDatabase(); + $databaseId = $data['databaseId']; + + $source = $this->client->call(Client::METHOD_POST, $this->getContainerUrl($databaseId), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getContainerIdParam() => ID::unique(), + 'name' => 'Source', + $this->getSecurityParam() => true, + ]); + + $related = $this->client->call(Client::METHOD_POST, $this->getContainerUrl($databaseId), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getContainerIdParam() => ID::unique(), + 'name' => 'Related', + $this->getSecurityParam() => true, + ]); + + $response = $this->client->call(Client::METHOD_POST, $this->getSchemaUrl($databaseId, $source['body']['$id']) . '/relationship', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getRelatedIdParam() => $related['body']['$id'], + 'type' => Database::RELATION_ONE_TO_ONE, + 'twoWay' => false, + 'key' => 'myRelation', + 'twoWayKey' => 'shouldBeIgnored', + 'onDelete' => Database::RELATION_MUTATE_CASCADE, + ]); + + $this->assertEquals(202, $response['headers']['status-code'], json_encode($response['body'])); + $this->assertEquals('', $response['body']['twoWayKey']); + + $this->waitForAttribute($databaseId, $source['body']['$id'], 'myRelation'); + } + + public function testMultipleOneWayRelationshipsToSameRelatedTable(): void + { + if (!$this->getSupportForRelationships()) { + $this->expectNotToPerformAssertions(); + return; + } + + $data = $this->setupDatabase(); + $databaseId = $data['databaseId']; + + $source = $this->client->call(Client::METHOD_POST, $this->getContainerUrl($databaseId), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getContainerIdParam() => ID::unique(), + 'name' => 'Source', + $this->getSecurityParam() => true, + ]); + + $related = $this->client->call(Client::METHOD_POST, $this->getContainerUrl($databaseId), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getContainerIdParam() => ID::unique(), + 'name' => 'Related', + $this->getSecurityParam() => true, + ]); + + $first = $this->client->call(Client::METHOD_POST, $this->getSchemaUrl($databaseId, $source['body']['$id']) . '/relationship', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getRelatedIdParam() => $related['body']['$id'], + 'type' => Database::RELATION_ONE_TO_ONE, + 'twoWay' => false, + 'key' => 'firstRelation', + 'onDelete' => Database::RELATION_MUTATE_SET_NULL, + ]); + + $this->assertEquals(202, $first['headers']['status-code'], json_encode($first['body'])); + $this->waitForAttribute($databaseId, $source['body']['$id'], 'firstRelation'); + + $second = $this->client->call(Client::METHOD_POST, $this->getSchemaUrl($databaseId, $source['body']['$id']) . '/relationship', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getRelatedIdParam() => $related['body']['$id'], + 'type' => Database::RELATION_ONE_TO_ONE, + 'twoWay' => false, + 'key' => 'secondRelation', + 'onDelete' => Database::RELATION_MUTATE_SET_NULL, + ]); + + $this->assertEquals(202, $second['headers']['status-code'], json_encode($second['body'])); + $this->waitForAttribute($databaseId, $source['body']['$id'], 'secondRelation'); + } + public function testManyToOneRelationship(): void { if (!$this->getSupportForRelationships()) {