Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 12 (modified by godoy, 13 years ago) (diff)


The docs clearly show how to insert/update a record, but not how to delete one.

If you have a reference to the object, simply call object.destroySelf() to remove it from the data base, provided it is not a foreign key or has any relationships depending on it.

If you do not have a reference to the object, simply use the delete class method:

class SampleSQLObject(SQLObject):
    # ...

If there are dependencies, you will have to delete the dependent records first (if at all.)

If there is a better way to do this, please update.

The way to cascade deletes is like this:

class Student(SQLObject):
    grade = sqlobject.ForeignKey('Grade', cascade=True)
class Grade(SQLObject):



It is better to use the "destroySelf" method instead of the delete method. So, you end up with:

student = Student.get(someid)

- Jorge Godoy

How do I go about deleting a bunch of related items?

I have a User and he has Items, I want to delete the User with his Items. Right now I go around and loop on the user.items and .destroySelf them, but that's inefficient SQL. I could just do "DELETE FROM item WHERE item.userID = x". Is there anyway to achieve it with SQLObject?

Using the SQL cascade feature is the way to go. This way, you delete the user and everything that allows cascading will also be deleted. You'll still get an error for tables that have records referencing said user and that doesn't allow cascading (then, when the transaction is rolled back, everything will still be there...).

By the way, it is better to ask questions on the mailing list since it is hard to find and answer questions here.

- Jorge Godoy