9

I can't find equivalent solution about Returning clause

DELETE FROM items
WHERE sub_item_id IN %(sub_item_ids)s
RETURNING item_id, sub_item_id, 

I did this in SQLAlchemy :

purge_family = session.query(ItemItem).filter(ItemItem.sub_item_id.in_(sub_item_ids))

.delete(synchronize_session=False)

I need to return item_id, sub_item_id set upon execution of DELETE statement.

7
  • Out of interest, why do you need that? Commented Jun 26, 2017 at 13:09
  • 1
    because i need to know (PosgreSql) , how many items are deleted. for example i have list of 10 items, after it ll only delete 4 items. then i continue to run other treatment Commented Jun 26, 2017 at 13:24
  • But Query.delete() already returns the count of matched rows. Commented Jun 26, 2017 at 13:26
  • yepp the count don't the values what i need to return Commented Jun 26, 2017 at 13:30
  • I still have my doubts that this is an XY problem. Supposedly the "other treatment" depends on the ids of the removed rows somehow? Commented Jun 26, 2017 at 13:35

1 Answer 1

10

To execute your query as is use Core constructs:

stmt = ItemItem.__table__.delete().\
    where(ItemItem.sub_item_id.in_(sub_item_ids)).\
    returning(ItemItem.item_id, ItemItem.sub_item_ids)

results = session.execute(stmt).fetchall()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.