Deleting an object
When you call the delete()
method of an entity instance, Pony marks the object as deleted. The object will be removed from the database during the following commit.
For example, this is how we can delete an order with the primary key equal to 123:
Order[123].delete()
Bulk delete
Pony supports bulk delete for objects using the delete()
function. This way you can delete multiple objects without loading them to the cache:
delete(p for p in Product if p.category.name == 'SD Card')
#or
Product.select(lambda p: p.category.name == 'SD Card').delete(bulk=True)
Note
Be careful with the bulk delete:
before_delete()
andafter_delete()
hooks will not be called on deleted objects.If an object was loaded into memory, it will not be removed from the
db_session()
cache on bulk delete.
Cascade delete
When Pony deletes an instance of an entity it also needs to delete its relationships with other objects. The relationships between two objects are defined by two relationship attributes. If another side of the relationship is declared as a Set
, then we just need to remove the object from that collection. If another side is declared as Optional
, then we need to set it to None
. If another side is declared as Required
, we cannot just assign None
to that relationship attribute. In this case, Pony will try to do a cascade delete of the related object.
This default behavior can be changed using the cascade_delete
option of an attribute. By default this option is set to True
when another side of the relationship is declared as Required
and False
for all other relationship types.
If the relationship is defined as Required
at the other end and cascade_delete=False
then Pony raises the ConstraintError
exception on deletion attempt.
Let’s consider a couple of examples.
The example below raises the ConstraintError
exception on an attempt to delete a group which has related students:
class Group(db.Entity):
major = Required(str)
items = Set("Student", cascade_delete=False)
class Student(db.Entity):
name = Required(str)
group = Required(Group)
In the following example, if a Person
object has a related Passport
object, then if you’ll try to delete the Person
object, the Passport
object will be deleted as well due to cascade delete:
class Person(db.Entity):
name = Required(str)
passport = Optional("Passport", cascade_delete=True)
class Passport(db.Entity):
number = Required(str)
person = Required("Person")