fix: ignore twoWayKey for one-way relationship columns

This commit is contained in:
Harsh Mahajan committed 2026-03-19 13:21:27 +05:30
1 parent 6b573524e7
commit ccd227df5c
2 files changed
+183 -1

No files matched your search

@@ -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()
) {
@@ -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()) {