Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLAlchemyPanel: UnicodeDecodeError for binary data #50

Open
MartinSchmidt123 opened this issue May 22, 2024 · 0 comments
Open

SQLAlchemyPanel: UnicodeDecodeError for binary data #50

MartinSchmidt123 opened this issue May 22, 2024 · 0 comments

Comments

@MartinSchmidt123
Copy link

Thanks for this great project. It is really helpful and simplifies the migration from django to fastapi.

I use SQLAlchemyPanel and have a mysql table with a binary column described by:

    filedata: Mapped[bytes] = mapped_column(LONGBLOB)

When updating this column with binary data, I receive the following error

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9d in position 15: invalid start byte

Traceback is

File "[..].venv/lib/python3.11/site-packages/debug_toolbar/panels/sqlalchemy.py", line 44, in after_execute
    self.add_query(str(context.engine.url), query)
  File "[..].venv/lib/python3.11/site-packages/debug_toolbar/panels/sql.py", line 110, in add_query
    self._queries.append((alias, jsonable_encoder(query)))
                                 ^^^^^^^^^^^^^^^^^^^^^^^
  File "[..].venv/lib/python3.11/site-packages/fastapi/encoders.py", line 289, in jsonable_encoder
    encoded_value = jsonable_encoder(
                    ^^^^^^^^^^^^^^^^^
  File "[..].venv/lib/python3.11/site-packages/fastapi/encoders.py", line 303, in jsonable_encoder
    jsonable_encoder(
  File "[..].venv/lib/python3.11/site-packages/fastapi/encoders.py", line 289, in jsonable_encoder
    encoded_value = jsonable_encoder(
                    ^^^^^^^^^^^^^^^^^
  File "[..].venv/lib/python3.11/site-packages/fastapi/encoders.py", line 318, in jsonable_encoder
    return ENCODERS_BY_TYPE[type(obj)](obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[..].venv/lib/python3.11/site-packages/fastapi/encoders.py", line 59, in <lambda>
    bytes: lambda o: o.decode(),
                     ^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9d in position 15: invalid start byte

A simplified example for a fastapi endpoint is

@router.get("/test_binary/")
def test_binary(session: Session = Depends(create_session)):
    
    import io
    import xlsxwriter
    xlb = io.BytesIO()
    workbook = xlsxwriter.Workbook(xlb)
    worksheet = workbook.add_worksheet("Test")
    worksheet.name = 'Worksheet'
    workbook.close()

    export = session.query(BinaryTable).filter([..]).first()
    export.filedata = xlb.getvalue()

    session.commit()

It seems that the bytes parameter cannot be logged.

Used versions:

  • fastapi==0.111.0
  • fastapi-debug-toolbar==0.6.3
  • sqlalchemy==2.0.30
  • xlsxwriter==3.2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant