4
votes

I need to define multiple modules that contain SQLAlchemy Declarative classes. I've written a function in each module called subclass_base() into which a Base instance of declarative_base() is passed after instantiation. The first module's subclass_base() call correctly subclasses the Base instance and the subclasses are visible from outside the function. The second module's call finishes without errors but from both within the function and outside of it all of the subclasses are reflected in Base.__subclasses__ only some of the time. Here is a minimal working example with only 1 class definition in each module:

modela.py

from sqlalchemy import Column, Integer, String
def subclass_base(Base):
    class Roles(Base):
        __tablename__ = 'roles'

        id = Column(Integer, primary_key=True)
        name = Column(String(32))

modelb.py

from sqlalchemy import Column, Integer, String
def subclass_base(Base):
    class Locations(Base):
         __tablename__ = 'locations'

        id = Column(Integer, primary_key=True)
        name = Column(String(64))

test.py

from sqlalchemy.ext.declarative import declarative_base
from modela import subclass_base as amod
from modelb import subclass_base as bmod

def test():
    count = 0
    while True:
        try:
            Base = declarative_base()

            amod(Base)

            bmod(Base)
            sc = [subclass.__name__ for subclass in Base.__subclasses__()]
            assert(len(sc) == 2)

            print('.', end='')
            count += 1
        except AssertionError:
            print("Failed after {} successful pass(es)".format(count))
            count = 0

I suspect this issue is me overlooking a specific issue of the metaclass work that goes on with declarative_base() but I can't seem to figure out what's happening. I'm also wondering if this is an inheritance issue. Is there a different architectural approach that I should take rather than using a function to subclass a single Base class?

1

1 Answers

5
votes

Don't define the class inside a function. Just define the Base in a single definition module then import that module from the other modules:

db.py

from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
Session = sessionmaker()

def bind_engine(engine):
    Base.metadata.bind = engine
    Session.configure(bind=engine)

modela.py

from sqlalchemy import Column, Integer, String
from db import Base

class Roles(Base):
    __tablename__ = 'roles'

    id = Column(Integer, primary_key=True)
    name = Column(String(32))

modelb.py

from sqlalchemy import Column, Integer, String
from db import Base

class Locations(Base):
    __tablename__ = 'locations'

    id = Column(Integer, primary_key=True)
    name = Column(String(64))

user_script.py

 from sqlalchemy import create_engine
 engine = create_engine('postgresql://user:pwd@server/database')
 from yourproject.db import bind_engine
 bind_engine(engine)