我有一个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_
我做错了什么?