I have a models
module that defines my declarative tables, like for example:
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class MyTable(Base):
__tablename__ = 'T_FOO'
__table_args__ = {'schema': 'BAR'}
id = Column('ID', Integer, primary_key=True)
Now, I'd like to dynamically change only the table name and/or schema name. But for clarity in my code, I also want to keep my declarative models like above.
Following this answer, I tried
MyTable.__table_args__['schema'] = 'TAZ' type(MyTable.__mapper__.class_.__name__, MyTable.__bases__, dict(MyTable.__dict__))
But this results in an
AssertionError
with stack trace (sqlalchemy
v1.0.9):Traceback (most recent call last): [...] File "[...]\sqlalchemy\orm\mapper.py", line 625, in __init__ self._configure_class_instrumentation() File "[...]\sqlalchemy\orm\mapper.py", line 1113, in _configure_class_instrumentation assert manager.class_ is self.class_
What am I doing wrong?