Hi all,I'm trying to get deletes and updates cascaded down from a parent
object to the child objects (connected by ForeignKey).

It all seems pretty simple in the docs, but I can't get it to work! I'm
using MySQL with the InnoDB engine, and have played with all the variation
of the onupdate, ondelete and cascade arguments I can think of.

The problem is that immediately before the DELETE command is sent to MySQL,
there are UPDATE commands nulling out the foreign key references of the
child objects, so MySQL doesn't trigger it's ON DELETE CASCADE action, and
for some reason cascade="all, delete-orphan" doesn't clean up the children
with NULL FKs.

Here's a simplified model:
users_table = Table('tg_user', metadata,
    Column('user_id', Integer, primary_key=True),
    Column('created', DateTime, default=datetime.now),
    mysql_engine='InnoDB',
)

hat_table = Table('hat', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', Unicode(100)),
    Column('user_id', Integer, ForeignKey('tg_user.user_id'),
        onupdate='CASCADE', ondelete='CASCADE'),
    mysql_engine='InnoDB',
)

class User(object):
    pass

class Hat(object):
    pass

mapper(User, users_table)

mapper(Hat, hat_table,
    properties = {
        'user': relation(User, backref="hats", cascade="all, delete,
delete-orphan"),
    }
)

And the log from SA when I do a User.delete:
 BEGIN
 UPDATE hat SET user_id=%s WHERE hat.id = %s
   [None, 1L]
 DELETE FROM tg_user WHERE tg_user.user_id = %s
   [1L]
 COMMIT

Any help would be much appreciated!

James

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to