Sqlalchemy delete subquery

前端 未结 1 355
鱼传尺愫
鱼传尺愫 2020-12-23 13:42

I am trying to delete some child rows using a filtered query without result:

sl = DBSession.query(Puesto.id).filter(Puesto.locales_id == id).subquery()
DBSes         


        
相关标签:
1条回答
  • 2020-12-23 14:11

    After looking in the source where your exception occurs I suggest trying this:

    sl = DBSession.query(Puesto.id).filter(Puesto.locales_id == id).subquery()
    DBSession.query(Servicio).filter(Servicio.puestos_id.in_(sl)) \
    .delete(synchronize_session='fetch')
    

    See the documentation of the delete method for what this means. Passing the fetch argument will basically run the query twice, once as a select and once as a delete.

    If running two queries is not desired, pass synchronize_session=False instead and then call session.expire_all() immediately after the delete to avoid having inconsistent state within the MetaData store.

    0 讨论(0)
提交回复
热议问题