#3454·cloudreve

[BUG] Under the PostgreSQL database, the file upload failed due to the foreign key constraint of audit_logs_files_audit_logs, and the file was deleted.

Author: SonderBaiLuCreated May 14, 2026Updated Sep 15, 2026
Labelsbug

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed. / 请使用英语提交,否则会被关闭。
  • Please do not modify this template :) and fill in all the required fields.

Cloudreve version

v4

Pro or Community Edition

Pro

Database type

PostgreSQL

Browser and operating system

No response

Steps to reproduce

Describe the bug I deployed Cloudreve with a brand-new PostgreSQL database. File uploads always fail, and the files are deleted immediately after the upload. The database logs show the same foreign key error every time an upload is attempted: the audit_logs table tries to insert a record that references a file ID that does not exist in the files table. The foreign key check fails, and the entire upload transaction is rolled back. Even after clearing the audit_logs table, the problem persists.

To Reproduce Steps to reproduce the behavior:

  1. Deploy a fresh PostgreSQL database using Docker Compose.
  2. Perform a fresh installation of Cloudreve (Pro) and let it create the tables automatically.
  3. Upload any file via the web interface.
  4. Check the PostgreSQL container logs. The foreign key error mentioned below will appear, and the file upload will eventually fail.

Expected behavior On a new database, the first file upload should successfully write the file record to the files table and simultaneously insert the corresponding operation log into the audit_logs table.

Screenshots or Logs The following foreign key error appears multiple times in the container logs for every upload: ERROR: insert or update on table "audit_logs" violates foreign key constraint "audit_logs_files_audit_logs" DETAIL: Key (file_audit_logs)=(4) is not present in table "files". STATEMENT: INSERT INTO "audit_logs" ("content", "correlation_id", "created_at", "file_audit_logs", "ip", "type", "updated_at", "user_audit_logs") VALUES (1,1,2, 3,3,4, 5,5,6, 7,7,8) RETURNING "id"

Even after deleting all data from the table, the same foreign key error still occurs:

ERROR: insert or update on table "audit_logs" violates foreign key constraint "audit_logs_files_audit_logs" DETAIL: Key (file_audit_logs)=(5) is not present in table "files".

Environment:

  • Cloudreve Pro v4
  • Deployment: Docker / Docker Compose
  • PostgreSQL: postgres:17-alpine / postgres:18-alpine (tried both)
  • Storage policy: local storage

Root Cause Analysis Preliminary analysis points to a transaction/sequence issue at the application level. During an upload, Cloudreve likely pre-fetches the next ID from the files table’s sequence (e.g., 4). After the actual insertion into the files table fails, that ID is discarded, but Cloudreve still uses the discarded ID when writing to audit_logs. This causes the foreign key constraint to block the audit log insertion, which in turn rolls back the entire transaction. This indicates that the foreign key constraint audit_logs_files_audit_logs on the audit_logs table does not match the application’s upload logic.

✔️ Expected Behavior

Upload files normally

❌ Actual Behavior

No response

Addition context information

No response