5

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?

Community
  • 1
  • 1
Kibour
  • 151
  • 1
  • 11
  • 1
    Why do you need to dynamically change the schema name? Are you trying to access two tables with the same structure but on different schema? Or are you trying to declaratively model several tables having something in common like a common schema and columns? – shimofuri Dec 01 '15 at 21:32
  • In fact, I created smaller "subtables" of far larger tables in order to fasten my queries, and put those subtables in a specific schema (source tables are located in various schemas). So the table structures themselves are identical. My framework should work on the original database without subtables, that's why I would like to dynamically change the schema and/or table name if I use such subtables. – Kibour Dec 02 '15 at 09:45

0 Answers0