我们有1个包含大量数据的表,DBA根据特定参数对其进行了分区。这意味着我最终得到了某种表名。以前很简单, 就像——Employee_TX, Employee_NYmodels.py
class Employee(Base):
__tablename__ = 'Employee'
name = Column...
state = Column...
现在,我不想为新分区的表创建50个新类,因为无论如何,我的列是相同的。
有没有一种模式,我可以创建一个类,然后在查询中动态使用它?session.query(<Tablename>).filter().all()
也许某种工厂模式或其他东西是我正在寻找的。
到目前为止,我已经尝试通过运行循环作为
for state in ['CA', 'TX', 'NY']:
class Employee(Base):
__qualname__ = __tablename__ = 'Employee_{}'.format(state)
name = Column...
state = Column...
但这不起作用,我得到一个警告 -SAWarning: This declarative base already contains a class with the same class name and module name as app_models.employee, and will be replaced in the string-lookup table.
此外,当我这样做时,它找不到生成的类from app_models import Employee_TX
这是一个烧瓶应用程序,以PostgreSQL作为后端,sqlalchemy用作ORM。
慕田峪4524236
BIG阳
相关分类