Say I have a schema of photo tagging, in postgreSQL.
It is pretty simple:
- a user has many photos
- a photo has many tags
I'm using SQL alchemy and trying to understand how to compose a filter to find, and delete, all the tags of all the photos of a specific user.
I know how to go about finding all the photos of a user:
specific_user_id = "1234"
DBSession = sessionmaker(bind=engine)
s = DBSession()
q = s.query(Photo).filter(Photo.user_id == specific_user_id)
q.delete()
How do I extend this to get all the tags. I can use a loop:
for photo in q.all():
q2 = s.query(Tag).filter(Tag.photo_id == photo.photo_id)
q2.delete()
But I'm looking to do this without the loop.