Skip to content

Latest commit

 

History

History
71 lines (54 loc) · 2.42 KB

File metadata and controls

71 lines (54 loc) · 2.42 KB

Bug Fix: SQLAlchemy Metadata Column Conflict

Issue

The backend was failing to start with the error:

sqlalchemy.exc.InvalidRequestError: Attribute name 'metadata' is reserved when using the Declarative API.

Root Cause

In the PDFDocument model, we added a column named metadata to store PDF metadata. However, metadata is a reserved attribute in SQLAlchemy's Declarative API used internally by the ORM.

Solution

Renamed the database column from metadata to pdf_metadata throughout the codebase.

Files Modified

backend/app.py

  1. PDFDocument Model (Line 82)

    • Changed: metadata = db.Column(db.JSON)
    • To: pdf_metadata = db.Column(db.JSON)
  2. PDFDocument.to_dict() Method (Line 95)

    • Changed: 'metadata': self.metadata
    • To: 'metadata': self.pdf_metadata
    • Note: API still returns metadata key for consistency
  3. upload_pdf() Function (Line 206)

    • Changed: metadata=pdf_info.get('metadata')
    • To: pdf_metadata=pdf_info.get('metadata')
  4. merge_pdfs() Function (Line 357)

    • Changed: metadata=pdf_info.get('metadata')
    • To: pdf_metadata=pdf_info.get('metadata')
  5. split_pdf() Function (Line 438)

    • Changed: metadata=pdf_info.get('metadata')
    • To: pdf_metadata=pdf_info.get('metadata')
  6. rotate_pdf() Function (Line 523)

    • Changed: metadata=pdf_info.get('metadata')
    • To: pdf_metadata=pdf_info.get('metadata')
  7. update_document() Function (Line 656)

    • Changed: document.metadata = data['metadata']
    • To: document.pdf_metadata = data['metadata']

API Compatibility

The API remains unchanged. The to_dict() method still returns the key as metadata in JSON responses, so no frontend changes are required.

Database Migration Required

If you have existing data, you'll need to run a migration to rename the column:

ALTER TABLE pdf_documents RENAME COLUMN metadata TO pdf_metadata;

Or drop and recreate the database (if no production data):

docker-compose down -v
docker-compose up -d

Testing

After applying this fix:

  1. Restart the backend: docker-compose restart backend celery_worker
  2. Verify services start without errors: docker-compose logs backend
  3. Test upload endpoint: curl -X POST http://localhost:5000/api/upload -F "file=@test.pdf"
  4. Verify metadata is returned correctly in API responses

Status

✅ Fixed - All references to metadata column updated to pdf_metadata