SQLAlchemy动态更改声明性表名

时间:2015-11-20 12:13:49

标签: python sqlalchemy

我有一个models模块,用于定义我的声明表,例如:

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)

现在,我想动态更改 表名和/或架构名称。但为了清楚我的代码,我还想保留上面的声明性模型。

  • answer之后,我尝试了

    MyTable.__table_args__['schema'] = 'TAZ'
    type(MyTable.__mapper__.class_.__name__, MyTable.__bases__, dict(MyTable.__dict__))
    

    但这会产生AssertionError堆栈跟踪(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_
    

    我做错了什么?

0 个答案:

没有答案