Since joining is not allowed on "delete" queries in Peewee, what is the best way to delete all records in table_2 that match a specific condition in related table_1?
Using a simple example, I want to achieve the equivalent of this:
  DELETE message.*
  FROM message
  JOIN user ON message.from_user_id = user.id
  WHERE user.name = "Joe";
                You should use subqueries for this type of thing, e.g.:
joe = User.select().where(User.username == 'Joe')
Message.delete().where(Message.from_user == joe).execute()
Let's say you want to delete all messages from "banned" users. You could write:
banned_users = User.select().where(User.is_banned == True)
Message.delete().where(Message.user.in_(banned_users)).execute()
                        If you're using Postgresql, you can use a raw query with the USING clause
name_to_delete = 'Joe'
query = Message.raw("""
    DELETE FROM message 
        USING user 
    WHERE 
        message.from_user_id = user.id AND
        user.name = %s
""", name_to_delete)
query.execute()
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With