0

I want to create a temp table from a result of CTE using SQLAlchemy.

Tables definition:

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) 

This is my CTE:

c_id_cte = (session.query(Data.c_id)).cte('c_id_cte')

I tried combining insert() with from_select() like this:

session.execute(CIdTmp.insert().from_select(['c_id'], c_id_cte))

But it produces this error:

AttributeError: type object 'CIdTmp' has no attribute 'insert'

1 Answer 1

1

Ok, I found out that I have to get a Table object to execute an insert expression. It can be done by using ___table__ on my model.

session.execute(CIdTmp.__table__.insert().from_select(['c_id'], c_id_cte))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.