Skip to content

Prevent sqlalchemy_adapter.Adapter from automatically creating tables #9

Closed

Description

@nonodev96

Is there a way to use it only as an intermediary without creating any tables?

I am using sqlalchemy_adapter.Adapter, and I have observed that it automatically creates the corresponding tables in the database during initialization.

My goal is to use the adapter strictly as an intermediary between SQLAlchemy and the system that consumes it, without having it manage or create any tables.

I would like to know:

Whether there is an option or parameter to disable automatic table creation.

Whether there is a recommended way to configure the adapter so that it does not call metadata.create_all() or create any database structures.

Any guidance or references would be appreciated.

engine = get_engine_postgres()

adapter_rbac = sqlalchemy_adapter.Adapter(engine)
# adapter_rebac = sqlalchemy_adapter.Adapter(engine, db_class=CasbinRuleReBAC)
# adapter_rebac = sqlalchemy_adapter.Adapter(engine, table_name=CasbinRuleReBAC.__tablename__)
Lib\site-packages\sqlalchemy_adapter\adapter.py:33: SAWarning: This declarative base already contains a class with the same class name and module name as sqlalchemy_adapter.adapter.CasbinRule, and will be replaced in the string-lookup table.        
  class CasbinRule(Base):
--- All Policies (p) ---
['alice', '/api/v1/items', 'GET']
--- All Role Assignments (g) ---

Activity

  1. added
    enhancementNew feature or request
    and removed
    questionFurther information is requested
    on Nov 16, 2025
  2. nonodev96 commented on Nov 17, 2025

    @nonodev96
    Author

    The bug has been fixed, but the warning remains.

    _dynamic_table = {}
    
    
    def create_casbin_rule_class(table_name):
        """
        Factory function to create a CasbinRule class with a custom table name.
    
        Args:
            table_name (str): Table name for the CasbinRule class.
    
        Returns:
            db_class (CasbinRule): The CasbinRule class.
        """
        if table_name in _dynamic_table:
            return _dynamic_table[table_name]
    
        class_name = "CasbinRule_" + table_name.replace("-", "_").replace(".", "_")
        
        def __str__(self):
            arr = [self.ptype]
            for v in (self.v0, self.v1, self.v2, self.v3, self.v4, self.v5):
                if v is None:
                    break
                arr.append(v)
            return ", ".join(arr)
    
        def __repr__(self):
            return '<CasbinRule {}: "{}">'.format(self.id, str(self))
    
        # Dynamically create the class using type() to ensure unique class name
        CasbinRule = type(
            class_name,
            (Base,),
            {
                "__tablename__": table_name,
                "__table_args__": {"extend_existing": True},
                "id": Column(Integer, primary_key=True),
                "ptype": Column(String(255)),
                "v0": Column(String(255)),
                "v1": Column(String(255)),
                "v2": Column(String(255)),
                "v3": Column(String(255)),
                "v4": Column(String(255)),
                "v5": Column(String(255)),
                "__str__": __str__,
                "__repr__": __repr__,
            }
        )
        
        _dynamic_table[table_name] = CasbinRule
        return CasbinRule
  3. hsluoyz commented on Nov 17, 2025

    @hsluoyz
    Member

    @nonodev96 create new issue, show your warning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions