Install & Compatibility
Where this runs
tested against v1.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 1.014s · 47.2MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 3.7s · import 0.918s · 45MB
46MB installed
● package 46MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Adapter
✓ from sqlalchemy_adapter import Adapter
✗ from casbin_sqlalchemy_adapter import Adapter
The library package name changed from `casbin-sqlalchemy-adapter` to `sqlalchemy-adapter`.
This quickstart demonstrates how to initialize the `SQLAlchemyAdapter` and integrate it with `pycasbin.Enforcer`. It uses an in-memory SQLite database for a self-contained example. Policy rules are added, saved to the database via the adapter, and then enforced. Ensure you have `pycasbin` installed alongside `sqlalchemy-adapter`.
import os
import casbin
import tempfile
from sqlalchemy_adapter import Adapter
# Create a dummy model.conf for the Enforcer
model_conf_content = '''
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
'''
# Write the model content to a temporary file
with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix=".conf") as model_file:
model_file.write(model_conf_content)
model_path = model_file.name
# Use an in-memory SQLite database for a simple, runnable example.
# For persistent storage, provide a file path or a full database URL
# e.g., 'sqlite:///test.db' or from an environment variable.
database_url = os.environ.get('SQLALCHEMY_DATABASE_URL', 'sqlite:///:memory:')
try:
# 1. Initialize the SQLAlchemy Adapter
# By default, create_table=True, so tables will be created if they don't exist.
adapter = Adapter(database_url)
# 2. Initialize Casbin Enforcer with the adapter
e = casbin.Enforcer(model_path, adapter)
# 3. Add policies
e.add_policy("alice", "data1", "read")
e.add_policy("bob", "data2", "write")
# 4. Save policies to the database (persists changes through the adapter)
e.save_policy()
print("Policies added and saved.")
# 5. Enforce policies
print(f"Alice can read data1: {e.enforce('alice', 'data1', 'read')}") # Expected: True
print(f"Bob can write data2: {e.enforce('bob', 'data2', 'write')}") # Expected: True
print(f"Alice can write data1: {e.enforce('alice', 'data1', 'write')}") # Expected: False
# 6. Load policies (useful if Enforcer was re-initialized or policy was changed elsewhere)
e.load_policy()
print(f"After reloading, Alice can read data1: {e.enforce('alice', 'data1', 'read')}") # Expected: True
except Exception as ex:
print(f"An error occurred: {ex}")
finally:
# Clean up the temporary model file
os.remove(model_path)
Upgrade
Version history
1.9.0latest on PyPI · released Nov 17, 2025
Audit
Dependencies
pycasbinrequiredCore authorization library that this adapter integrates with.
sqlalchemyrequiredORM and database toolkit used for database interactions.