来自 SELECT 的 sqlalchemy INSERT
sqlalchemy INSERT from SELECT
我有麻烦了。
来自 SELECT 构造的 INSERT 会编译,但不会执行。没有任何错误。如果查看日志文件,您不会 SQL ISERT INTO ... SELECT ... FROM ....
这是我的代码:
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
)
有人知道为什么不执行吗?
此回答已关闭。我错过了 execute
声明。
insert().from_select() 只生成一个文本,SQL 查询。
应按如下方式使用:
session.execute(insert(...).from_select(...)
我有麻烦了。 来自 SELECT 构造的 INSERT 会编译,但不会执行。没有任何错误。如果查看日志文件,您不会 SQL ISERT INTO ... SELECT ... FROM ....
这是我的代码:
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
)
有人知道为什么不执行吗?
此回答已关闭。我错过了 execute
声明。
insert().from_select() 只生成一个文本,SQL 查询。
应按如下方式使用:
session.execute(insert(...).from_select(...)