[Python] SQLAlchemy - copy schema and data of subquery to another database

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to copy data from a subquery from postgres (from_engine) to sqlite database. I can achieve this for copying a table using following command:

    smeta = MetaData(bind=from_engine)
    table = Table(table_name, smeta, autoload=True)
    table.metadata.create_all(to_engine)


    However, I am not sure how to achieve the same for a subquery statement.

    Edit: Follow up on the answer. Once I have created the table I want to create a subquery stmt as follows:

    table = Table("newtable", dest_metadata, *columns)
    stmt = dest_session.query(table).subquery();


    However, the last stmt ends up with error cursor.execute(statement, parameters) sqlalchemy.exc.ProgrammingError: (ProgrammingError) relation "newtable" does not exist LINE 3: FROM newtable) AS anon_1

    Continue reading...

Compartilhe esta Página