biigle/user-storage

Fix duplicate key error during upload

Opened this issue · 0 comments

Now and then there is an error like this:

message" => """
    SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates unique constraint "storage_request_files_path_storage_request_id_unique"
    DETAIL:  Key (path, storage_request_id)=(myfilenamepath, 2128) already exists. (Connection: pgsql, SQL: insert into "storage_request_files" ("path", "size", "storage_request_id") values (myfilenamepath, 12071687, 2128) returning "id") ◀
    """
  "context" => array:2 [▼
    "userId" => 0000
    "exception" => array:6 [▼
      "class" => "Illuminate\Database\UniqueConstraintViolationException"
      "message" => """
        SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates unique constraint "storage_request_files_path_storage_request_id_unique"
        DETAIL:  Key (path, storage_request_id)=(myfilenamepath, 2128) already exists. (Connection: pgsql, SQL: insert into "storage_re ▶
        """
      "code" => 23505
      "file" => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:796"
      "trace" => array:71 [▼
        0 => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:755"
        1 => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:424"
        2 => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:394"
        3 => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/PostgresProcessor.php:24"
        4 => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:3387"
        5 => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1966"
        6 => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1333"
        7 => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1298"
        8 => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1137"
        9 => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php:342"
        10 => "/var/www/vendor/laravel/framework/src/Illuminate/Support/helpers.php:307"
        11 => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php:343"
        12 => "/var/www/vendor/biigle/user-storage/src/Http/Controllers/Api/StorageRequestFileController.php:89"
        13 => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php:30"
        14 => "/var/www/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:469"
        15 => "/var/www/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:353"
        16 => "/var/www/vendor/biigle/user-storage/src/Http/Controllers/Api/StorageRequestFileController.php:116"

Figure out why this happens and fix it. Maybe a request times out and the file is created in the backend but the frotend retries? A naive solution might be to check if the file already exists and send a special error message (or immediate success) that can be handled by the frontend. But maybe something else is amiss and should be fixed instead of patching just the symptom.

The request was sent by Safari. Maybe this is part of the problem.