Sqlalchemy INSERT from SELECT

I have a problem's. INSERT from SELECT is compiled but not executed. There are no errors. Looking at the log file, you won't be SQL like ISERT INTO ... SELECT ... FROM ....

This is my code:

    DBSession.query(ProductMediaGalleryArchive)\
        .filter(ProductMediaGalleryArchive.product_id.in_(pack))\
        .delete(synchronize_session=False)

    query = DBSession.query(
            ProductMediaGallery.code,
            ProductMediaGallery.filename,
            ProductMediaGallery.mimetype,
            ProductMediaGallery.sha1hash,
            ProductMediaGallery.position,
            ProductMediaGallery.excluded,
            ProductMediaGallery.created,
            ProductMediaGallery.updated,
            ProductMediaGallery.id,
            ProductMediaGallery.is_rollover,
            Product.code.label('product_code'),
        ).join((Product, Product.id==ProductMediaGallery.product_id))\
        .filter(ProductMediaGallery.product_id.in_(pack))

    insert(ProductMediaGalleryArchive).from_select(
        (
            ProductMediaGalleryArchive.code,
            ProductMediaGalleryArchive.filename,
            ProductMediaGalleryArchive.mimetype,
            ProductMediaGalleryArchive.sha1hash,
            ProductMediaGalleryArchive.position,
            ProductMediaGalleryArchive.excluded,
            ProductMediaGalleryArchive.created,
            ProductMediaGalleryArchive.updated,
            ProductMediaGalleryArchive.id,
            ProductMediaGalleryArchive.is_rollover,
            ProductMediaGalleryArchive.product_code
        ),
        query
    )

      

Does anyone know why it hasn't been executed?

+3


source to share


1 answer


This answer is closed. I missed the instruction execute

. insert (). from_select () generates only text, SQL query. It should be used like this:



session.execute(insert(...).from_select(...)

      

+6


source







All Articles