[Sqlalchemy-tickets] Issue #3980: TypeError: issubclass() arg 1 must be a class (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
From: Anthony O. <iss...@bi...> - 2017-05-08 08:50:37
|
New issue 3980: TypeError: issubclass() arg 1 must be a class https://bitbucket.org/zzzeek/sqlalchemy/issues/3980/typeerror-issubclass-arg-1-must-be-a-class Anthony Oduor: In implementing a multi-tenant system using PostreSQL schemas... When A new customer signs-up I create a new schema when they activate their account. Here is the code for the function that creates the schema and even put's in a dummy record. ``` #!python settings = request.registry.settings engine = get_engine(settings) Session = sessionmaker(engine, expire_on_commit=False) Session.configure(bind=engine) session = Session() session.begin(subtransactions=True) try: engine.execute(CreateSchema(tenant_id)) metadata = MetaData(schema=tenant_id) declared_base = declarative_base(bind=engine, name='NewTenantBase', metadata=metadata) NewTenantBase = automap_base(declarative_base=declared_base) NewTenantBase.metadata.schema = tenant_id # NewTenantBase.metadata.reflect(bind=engine) NewTenantBase.prepare(engine, reflect=False) configure_mappers() class MyBooks(BooksMixin, NewTenantBase): __tablename__ = 'mybooks' # mapped classes are ready MyBooks = NewTenantBase.classes.mybooks NewTenantBase.metadata.create_all() try: # set the search path session.execute("SET search_path TO %s" % tenant_id) session.add(MyBooks(name='Fiction', description='You know what fiction are')) session.flush() session.execute("commit") except Exception as e: traceback.print_exc() session.rollback() session.commit() except Exception as e: traceback.print_exc() session.rollback() ``` This using Pyramid-Websauna framework. The first signup goes well and everything is setup properly in the new schema without an error. The problem is subsequent signups don't go as well. I get the error below which happens after the new schema has been created. ``` #!python TypeError('issubclass() arg 1 must be a class',) Traceback (most recent call last): File "/opt/mapylons/myproject/myapp/myapp/tasks.py", line 80, in create_schema NewTenantBase.prepare(engine, reflect=False) File "/opt/mapylons/myproject/venv3.5/lib/python3.5/site-packages/sqlalchemy/ext/automap.py", line 787, in prepare generate_relationship) File "/opt/mapylons/myproject/venv3.5/lib/python3.5/site-packages/sqlalchemy/ext/automap.py", line 894, in _relationships_for_fks local_cls, referred_cls): TypeError: issubclass() arg 1 must be a class ``` The interesting bit is that is when I restart the server between each signup, they will all go well and the customer schema setup will go on without a hitch. How can I make this multi-tenancy work properly without literally restarting `pserve` after each customer signup? ...which would be quite a hack job |