long2ice / asyncmy

A fast asyncio MySQL/MariaDB driver with replication protocol support
https://github.com/long2ice/asyncmy
Apache License 2.0
230 stars 25 forks source link

Using IntEnum as an argument fails with TypeError #73

Open oktavlachs opened 11 months ago

oktavlachs commented 11 months ago

When using IntEnum values, the following Exception occurs.

  File "/code/.venv/lib/python3.11/site-packages/sqlalchemy/util/_compat_py3k.py", line 44, in __aexit__
    await self.gen.athrow(typ, value, traceback)
  File "/code/.venv/lib/python3.11/site-packages/sqlalchemy/dialects/mysql/asyncmy.py", line 188, in _mutex_and_adapt_errors
    yield
  File "/code/.venv/lib/python3.11/site-packages/sqlalchemy/dialects/mysql/asyncmy.py", line 104, in _execute_async
    result = await self._cursor.execute(operation, parameters)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "asyncmy/cursors.pyx", line 177, in execute
  File "asyncmy/cursors.pyx", line 155, in asyncmy.cursors.Cursor.mogrify
  File "asyncmy/cursors.pyx", line 128, in genexpr
  File "asyncmy/cursors.pyx", line 128, in genexpr
  File "asyncmy/connection.pyx", line 421, in asyncmy.connection.Connection.literal
  File "asyncmy/connection.pyx", line 414, in asyncmy.connection.Connection.escape
  File "asyncmy/converters.pyx", line 11, in asyncmy.converters.escape_item
  File "asyncmy/converters.pyx", line 26, in asyncmy.converters.escape_item
TypeError: Argument 'value' has incorrect type (expected str, got IdentityType)

I found you fixed this for string Enum already here.

Can you do the same for IntEnum? :pray:

Thanks for your work, I am looking forward to seeing this in production. :+1:

oktavlachs commented 11 months ago

I was using this in the context of FastAPI + SQLModel and solved it by using the SQLAlchemy Column class.

Column(Enum(IdentityType))

oktavlachs commented 11 months ago

@long2ice Is there anything left for asnycmy from your perspective? Otherwise I would close this

oktavlachs commented 11 months ago

@long2ice Actually, my workaround does not behave in the way I would like to. The SQLAlchemy Enum column type maps onto the enum column type of MySQL instead of integet. This column type is not supported by all DB technologies and actually I would prefer to map onto integer.

So I would say this issue is still valid.

oktavlachs commented 11 months ago

For anyone else facing this and looking for a solution like I did, please find here a way to inject an IntEnum type in SQLAlchemy yourself:

https://stackoverflow.com/questions/33612625/how-to-model-enums-backed-by-integers-with-sqlachemy

yuchi518 commented 9 months ago

I also look for a solution about this problem. Because my team's project used auto-map data model, it seems not be possible to use the solution which @oktavlachs provided?

Could be possible to fix this issue?

Very Thanks.