sqlalchemy INSERT来自SELECT

时间:2015-05-26 15:22:52

标签: python sqlalchemy pyramid

我遇到了麻烦。 SELECT构造的INSERT编译,但不执行。没有任何错误。如果查看日志文件,您就不会像ISERT INTO ... SELECT ... FROM ....

那样获得SQL

这是我的代码:

    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
    )

有人知道为什么没有表演吗?

1 个答案:

答案 0 :(得分:6)

这个答案已经结束。我错过了execute声明。 insert()。from_select()只生成一个文本,SQL查询。 它应该如下使用:

session.execute(insert(...).from_select(...)
相关问题