diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index f609f93f2f..dfd2d1c093 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -2739,9 +2739,15 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/documents') fn() => $dbForProject->getDocument('database_' . $database->getInternalId() . '_collection_' . $relatedCollection->getInternalId(), $relation->getId()) ); - $type = $current->isEmpty() - ? Database::PERMISSION_CREATE - : Database::PERMISSION_UPDATE; + if ($current->isEmpty()) { + $type = Database::PERMISSION_CREATE; + + if (isset($relation['$id']) && $relation['$id'] === 'unique()') { + $relation['$id'] = ID::unique(); + } + } else { + $type = Database::PERMISSION_UPDATE; + } $checkPermissions($relatedCollection, $relation, $type); } @@ -3304,7 +3310,18 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents/:docum 'database_' . $database->getInternalId() . '_collection_' . $relatedCollection->getInternalId(), $relation->getId() )); - $checkPermissions($relatedCollection, $relation, $oldDocument); + + if ($oldDocument->isEmpty()) { + $type = Database::PERMISSION_CREATE; + + if (isset($relation['$id']) && $relation['$id'] === 'unique()') { + $relation['$id'] = ID::unique(); + } + } else { + $type = Database::PERMISSION_UPDATE; + } + + $checkPermissions($relatedCollection, $relation, $oldDocument, $type); } } } diff --git a/app/workers/databases.php b/app/workers/databases.php index 3f14c12eb5..9a62f9fa97 100644 --- a/app/workers/databases.php +++ b/app/workers/databases.php @@ -217,7 +217,9 @@ class DatabaseV1 extends Worker throw new Exception('Failed to delete Attribute'); } } + $dbForProject->deleteDocument('attributes', $attribute->getId()); + if (!$relatedAttribute->isEmpty()) { $dbForProject->deleteDocument('attributes', $relatedAttribute->getId()); }