Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy INSERT from SELECT

I have a trouble. The INSERT from SELECT construction compiles, but not performed. There aren't any error. If look to the log file, you won't SQL like ISERT INTO ... SELECT ... FROM ....

This is 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
    )

Anybody know why doesn't performed it?

like image 734
isrodick Avatar asked Mar 16 '23 22:03

isrodick


1 Answers

This answer is closed. I've missed the execute statement. insert().from_select() generates only a text, SQL query. It should be used as follows:

session.execute(insert(...).from_select(...)
like image 50
isrodick Avatar answered Apr 01 '23 03:04

isrodick