Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy: Using delete/update with a join query

Using Flask-SQLAlchemy, I would like to delete rows in table Questions based on the values of another table which is linked to table Topic which then is linked to table Subject with foreign keys. I tried this query:

db.session.query(Questions).join(Topic)join(Subject).filter(Subject.account_id==current_user.id).delete()

However, I receive an error:

InvalidRequestError: Can't call Query.update() or Query.delete() when join(), outerjoin(), select_from(), or from_self() has been called

So from this I suppose I cannot use .delete() with .join()

Is there a workaround for this issue? Thanks.

like image 789
Pav Sidhu Avatar asked Aug 18 '15 14:08

Pav Sidhu


Video Answer


1 Answers

You don't have to use join for your query, you may done it somehow like

db.session.query(Post).filter(Post.user_id==current_user.id).delete()

Assuming your Post have a user_id column.

Join tables would not know which table to delete, Post or User, because it actually have a convoluted middle table constructed, and query from it.

like image 176
socrates Avatar answered Sep 20 '22 07:09

socrates