[英]Insert into temp table using SQLAlchemy
我想使用 SQLAlchemy 从 CTE 的结果创建一个临时表。
表定义:
class Data(Base):
__tablename__ = 'data'
c_id = Column(Integer, primary_key=True)
# ...
# temp table
class CIdTmp(Base):
__tablename__ = '#c_id_tmp'
c_id = Column(Integer, primary_key=True)
这是我的 CTE:
c_id_cte = (session.query(Data.c_id)).cte('c_id_cte')
我尝试将insert()
与from_select()
结合起来,如下所示:
session.execute(CIdTmp.insert().from_select(['c_id'], c_id_cte))
但它会产生这个错误:
AttributeError: type object 'CIdTmp' has no attribute 'insert'
好的,我发现我必须得到一个Table
object 来执行insert
表达式。 它可以通过在我的 model 上使用___table__
来完成。
session.execute(CIdTmp.__table__.insert().from_select(['c_id'], c_id_cte))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.