如果两个SQLAlchemy模型必须参与相同的会话,是否需要从declarative_base()
的同一实例继承?导入两个或多个定义SQLAlchemy模型的模块时,情况可能如此。必须有两个SQLAlchemy声明模型共享相同的declarative_base()吗?
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class SomeClass(Base):
__tablename__ = 'some_table'
id = Column(Integer, primary_key=True)
name = Column(String(50))
Base2 = declarative_base()
class AnotherClass(Base2):
__tablename__ = 'another_table'
id = Column(Integer, primary_key=True)
name = Column(String(50))
您也可以使用带有binds = {Class:database,...}参数的单个Session。 – joeforker 2009-10-19 18:30:05
当然。但是,这种绑定字典在变得庞大时很难维护。虽然我说从几个元数据对象自动构造它。 – 2009-10-20 03:52:03