cascading deletes with relatedjoin

0 views
Skip to first unread message

qhar...@gmail.com

unread,
Apr 6, 2006, 6:11:03 PM4/6/06
to TurboGears
The cascade functionality of SQLObject seems to not apply to related
joins. Having to manually cleanup pointers back to deleted records
seems silly. I've looked through archives for both TG and SQLObject and
seen a lot of people asking about this funcitonality, but no
explanation as to how to approximate this functionality. Thoughts?

-QH-

Robin Haswell

unread,
Apr 7, 2006, 3:11:31 AM4/7/06
to turbo...@googlegroups.com
Seems obvious, but have you considered using a transactional DB, eg
MySQL with InnoDB?

-Rob

BJörn Lindqvist

unread,
Apr 7, 2006, 9:03:51 AM4/7/06
to turbo...@googlegroups.com

Just alther the tables that sqlobject automagically creates. I.e. I
have a RelatedJoin between Person and Keyword. Using tg-admin sql sql
I can see that the intermediate table between Person and Keyword that
sqlobject would create looks like this:

CREATE TABLE keyword_person (
keyword_id INT NOT NULL,
person_id INT NOT NULL
);

Then I just alter it to add the foreign key contraints:

alter table keyword_person
add foreign key (keyword_id) references keyword(id) on delete cascade;

alter table keyword_person
add foreign key (person_id) references person(id) on delete cascade;

It's dirt simple.

--
mvh Björn

qhar...@gmail.com

unread,
Apr 7, 2006, 11:47:51 AM4/7/06
to TurboGears
>...have you considered using a transactional DB, eg MySQL with InnoDB?

Nope, I hadn't. About the only thing I know about transactional
databases is that they are generally considered a good thing. I'm
pretty new to working with DB programming, and all of my expereince has
been with ones that don't support transactions so using that feature is
somehwat outside of my ken. I'll look into it though, thanks for the
suggestion.

-QH-

qhar...@gmail.com

unread,
Apr 7, 2006, 11:51:23 AM4/7/06
to TurboGears
> Just alther the tables that sqlobject automagically creates...

<snip useful example stuff>

I had no idea tg-admin could let me look at that sort of stuff. Handy.
In other "framework" systems I've worked with, directly manipulating
the "automagic" elements had a tendency to break stuff so I've learned
to avoid it. So, if that's not the case with TG, you're right, it's
dirt simple. Thanks!

-QH-

Robin Haswell

unread,
Apr 8, 2006, 4:03:09 PM4/8/06
to turbo...@googlegroups.com
This solution is a more fleshed out version of my solution. You need a
DB which supports ON DELETE CASCADE, which generally are transactional.
MySQL with InnoDB tables supports this, PostGres will almost certainly
support this, however I'm not so sure about SQLite.

-Rob

Alberto Valverde

unread,
Apr 8, 2006, 4:19:53 PM4/8/06
to turbo...@googlegroups.com

On Apr 8, 2006, at 10:03 PM, Robin Haswell wrote:

>
> This solution is a more fleshed out version of my solution. You need a
> DB which supports ON DELETE CASCADE, which generally are
> transactional.
> MySQL with InnoDB tables supports this, PostGres will almost certainly
> support this, however I'm not so sure about SQLite.

Yep, Postgres surely does.


Alberto

Max Ischenko

unread,
Apr 9, 2006, 9:34:03 AM4/9/06
to TurboGears
One REALLY crude solution that I use is:

def destroySelfPatched(join):
tableName = join.kw['intermediateTable']
joinColumn = join.kw['joinColumn']
def wrapper(self):
cls = self.__class__
super(cls, self).destroySelf()
sql = "DELETE FROM %s WHERE %s = %d" % \
(tableName, joinColumn, self.id)
self._connection.query(sql)
return wrapper

And

class Foobar(SQLObject):
tags = RelatedJoin(...)
destroySelf = destroySelfPatched(tags)

Reply all
Reply to author
Forward
0 new messages