pycasbin / async-sqlalchemy-adapter

Async SQLAlchemy Adapter for PyCasbin
https://github.com/casbin/pycasbin
Apache License 2.0
7 stars 6 forks source link

AttributeError: 'AsyncEngine' object has no attribute '_run_ddl_visitor' #4

Closed wu-clan closed 1 year ago

wu-clan commented 1 year ago

Please note that this code will raise an error:AttributeError

https://github.com/pycasbin/async-sqlalchemy-adapter/blob/7ad2de0676c258bf36cc29fb45069f5684c4f684/casbin_async_sqlalchemy_adapter/adapter.py#L84

The correct way to open in asynchronous should be:

async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.drop_all)

For detailed examples, please see:https://docs.sqlalchemy.org/en/14/_modules/examples/asyncio/async_orm.html

casbin-bot commented 1 year ago

@techoner @Nekotoxin

hsluoyz commented 1 year ago

@wu-clan Hi, can you make a PR to fix it?

wu-clan commented 1 year ago

There is no case for calling asynchronous use in the constructor

hsluoyz commented 1 year ago

@wu-clan thanks for the explanation. I mean, can you make a PR to fix what you said?

wu-clan commented 1 year ago

Hi, @hsluoyz

This doesn't seem to be a direct fix, as its current logic is to automatically create the casbin_rule table

unless the table creation action is changed to be performed manually

hsluoyz commented 1 year ago

@Wrapping-2000 do you understand what he said? If yes, please do code change

wu-clan commented 1 year ago

Hi, @hsluoyz

I changed the automatic table creation to manual and added a warning, if there is no better solution

and includes some other fixes, please check: #5

cwp0 commented 1 year ago

@wu-clan Did your test result pass?

cwp0 commented 1 year ago

@wu-clan I didn't encounter your error, can you tell me how you encountered the AttributeError?

wu-clan commented 1 year ago

@wu-clan Did your test result pass?

Yes, I tested sqlalcehmy 1.4.48 and 2.0.8

wu-clan commented 1 year ago

@wu-clan I didn't encounter your error, can you tell me how you encountered the AttributeError?

This error is raised when sqlalchemy version 2.0.x and async_sessionmaker() are used globally, together with the current version

My console printing (Summary):

File "/home/xx/.virtualenvs/my_project/lib/python3.10/site-packages/casbin_async_sqlalchemy_adapter/adapter.py", line 93, in __init__
    Base.metadata.create_all(self._engine)
  File "/home/xx/.virtualenvs/my_project/lib/python3.10/site-packages/sqlalchemy/sql/schema.py", line 5581, in create_all
    bind._run_ddl_visitor(
AttributeError: 'AsyncEngine' object has no attribute '_run_ddl_visitor'