python - Use strings in column_property queries
问题描述
I use sqlalchemy and have classes for two tables that are one-to-many related. And want to use column_property
in both of their definitions to add calculated fields.
I can reference Tbl1
in column_property
of Tbl2
because it is defined before Tbl2
, but I can't do it in Tbl1
.
It there any way to achieve this with using string table/column names or filtering using strings, rather than referring by class?
class Tbl1(Base):
__tablename__ = 'tbl1'
handle = Column(Integer, primary_key=True)
name = Column(String)
verified = Column(Boolean)
published = column_property(
exists()
.where(and_(
Tbl2.tbl1_handle == handle, # <- At this point Tbl2 is not defined yet
Tbl2.published.is_(True),
))
)
class Tbl2(Base):
__tablename__ = 'tbl2'
handle = Column(Integer, primary_key=True)
name = Column(String)
published = Column(Boolean)
tbl1_handle = Column('tbl1', ForeignKey('tbl1.handle'))
tbl1 = relationship('Tbl1', backref='tbl2s')
verified = column_property(
exists()
.where(and_(
Tbl1.handle == tbl1_handle,
Tbl1.verified.is_(True),
))
)
解决方案
推荐阅读
- ios - 在 SwiftUI List 中过滤 @Published 数组删除列表中的元素
- python - 如何在 vscode 中启用 python 片段?
- python - 在 for 循环后重置所有变量
- reactjs - 对tomcat中的spring boot白标签错误页面做出反应
- regression - 在具有有序概率的多方程模型中查找边际效应 - cmp
- python - 一元特征和成本量(计算机视觉)
- c++ - 合并排序算法不起作用(c ++),面临输出没有意义的问题
- javascript - 切换按钮不起作用 - 使用 Javascript 功能
- c# - 为什么在包含 .Query() 时 EF Core 3 不将集合标记为已加载?
- amazon-web-services - AWS ELB 不断返回 404 健康检查失败,代码如下:[404]