Casbin Async SQLAlchemy Adapter
raw JSON → 1.17.0 verified Fri May 01 auth: no python
Asynchronous SQLAlchemy adapter for PyCasbin, enabling policy storage in relational databases with async support. Current version 1.17.0, requires Python >=3.7. Released regularly alongside PyCasbin releases.
pip install casbin-async-sqlalchemy-adapter Common errors
error ModuleNotFoundError: No module named 'casbin_async_sqlalchemy_adapter' ↓
cause Library not installed or installed without async SQLAlchemy driver.
fix
pip install casbin-async-sqlalchemy-adapter aiosqlite
error AttributeError: 'Adapter' object has no attribute 'create_table' ↓
cause Using an outdated version or incorrect import path.
fix
Import Adapter from top-level and ensure version >=1.0.0.
error sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'sqlite:///test.db' ↓
cause Using sync SQLAlchemy URL without async driver prefix.
fix
Use 'sqlite+aiosqlite:///test.db' for async SQLite.
Warnings
breaking Adapter import path changed from submodule to top-level. ↓
fix Use 'from casbin_async_sqlalchemy_adapter import Adapter' instead of 'from casbin_async_sqlalchemy_adapter.adapter import Adapter'.
gotcha Adapter does not auto-create table; must call create_table() explicitly. ↓
fix Call await adapter.create_table() once before using the enforcer.
gotcha Requires an async SQLAlchemy driver (e.g., aiosqlite for SQLite, asyncpg for PostgreSQL). Sync drivers will fail. ↓
fix Use a compatible async driver URL (e.g., 'postgresql+asyncpg://...').
Imports
- Adapter wrong
from casbin_async_sqlalchemy_adapter.adapter import Adaptercorrectfrom casbin_async_sqlalchemy_adapter import Adapter
Quickstart
import asyncio
from casbin import Enforcer
from casbin_async_sqlalchemy_adapter import Adapter
async def main():
adapter = Adapter('sqlite+aiosqlite:///test.db')
# Initialize adapter (creates table)
await adapter.create_table()
e = Enforcer('path/to/model.conf', adapter)
# ... use enforcer
await e.enforce('alice', 'data1', 'read')
asyncio.run(main())