PictoPy icon indicating copy to clipboard operation
PictoPy copied to clipboard

BUG: Renaming an album to an existing name returns 400 with raw SQLite error

Open Prakhar1808 opened this issue 1 month ago • 3 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

What happened?

What happened?

Problem Statement

In backend/app/routes/albums.py, album names have a UNIQUE constraint in the SQLite schema (album_name TEXT UNIQUE).

When creating an album (POST /albums/ or POST /albums/from-memory), the backend checks if an album with that name already exists and raises _album_exists(body.name), returning HTTP 409 Conflict with a clean error message:

{
  "detail": {
    "success": false,
    "error": "Album Already Exists",
    "message": "Album '<name>' is already in the database."
  }
}

However, when renaming an album via PUT /albums/{album_id} (update_album), there is no validation check against existing album names before attempting the update.

When a user attempts to rename an album to a name that already belongs to another album:

  1. db_update_album triggers sqlite3.IntegrityError: UNIQUE constraint failed: albums.album_name.
  2. The route catches the exception in a generic except Exception as e block and returns HTTP 400 Bad Request.
  3. The response body leaks the internal raw SQLite table and column names:
{
  "detail": {
    "success": false,
    "error": "Failed to Update Album",
    "message": "UNIQUE constraint failed: albums.album_name"
  }
}

Steps to Reproduce

Run the following script using the backend test client:

curl -X POST http://localhost:52123/albums/ \
            -H "Content-Type: application/json" \
            -d '{"name":"Trip 2024","description":"","is_locked":false}'

curl -X POST http://localhost:52123/albums/ \
            -H "Content-Type: application/json" \
            -d '{"name":"Work","description":"","is_locked":false}'

# renaming the album using ID (don't forget to put the album ID)
curl -X PUT http://localhost:52123/albums/<PASTE_WORK_ALBUM_ID_HERE> \
            -H "Content-Type: application/json" \
            -d '{"name":"Trip 2024","description":"","is_locked":false}'

output which confirms

{"detail":{"success":false,"message":"UNIQUE constraint failed: albums.album_name","error":"Failed to Update Album"}}

Actual vs Expected Behavior

Endpoint Actual Status & Message Expected Status & Message
PUT /albums/{album_id} (Rename to collision) 400 Bad Request — "UNIQUE constraint failed: albums.album_name" 409 Conflict — "Album '<name>' is already in the database."

Screenshots

the bug Image


Root Cause

In backend/app/routes/albums.py (lines 202–248), update_album does not check db_get_album_by_name(body.name) before calling db_update_album().

Proposed Solution

In backend/app/routes/albums.py, validate the requested album name before calling db_update_album:

existing_album = db_get_album_by_name(body.name)
if existing_album and existing_album["album_id"] != album_id:
    raise _album_exists(body.name)

(Checking existing_album["album_id"] != album_id ensures that updating an album without changing its name—such as updating its description or lock status—continues to succeed).

Additionally, add a unit test case in backend/tests/test_albums.py asserting that updating an album to an existing name returns 409 Conflict.

I have verified the reproduction and solution locally, and would love to be assigned to submit a PR!

Record

  • [x] I agree to follow this project's Code of Conduct

Prakhar1808 avatar Oct 12 '26 13:10 Prakhar1808

👋 Thanks for opening this issue! The maintainers will review it shortly. Till then, do not open any PR. This is a strict rule we like to follow around here :)

github-actions[bot] avatar Oct 12 '26 13:10 github-actions[bot]

⚠️ Potential Duplicate Issue (Semantic Match)

This issue appears semantically similar to the following issues in AOSSIE-Org/PictoPy:

  1. BUG: fix: return 404 when removing image not in album (#1560, closed) https://github.com/AOSSIE-Org/PictoPy/issues/1560 Similarity: 70.7%

  2. BUG: Proper reporting of duplicate album error. (#112, closed) https://github.com/AOSSIE-Org/PictoPy/issues/112 Similarity: 68.9%

  3. BUG: SQLite Foreign Key Cascade Not Enforced Due to Missing PRAGMA on Connections (#867, closed) https://github.com/AOSSIE-Org/PictoPy/issues/867 Similarity: 62.2%

Please review before proceeding.

github-actions[bot] avatar Oct 12 '26 13:10 github-actions[bot]

Please go ahead @Prakhar1808, + Expected Status & Message should be "An album with the name <name> already exists, would you like to save this as <name>1"

rohan-pandeyy avatar Oct 12 '26 16:10 rohan-pandeyy