I create a child table and a parent table. The latter holds a relation
to the child with delete-orphan cascade.
When I delete _the first_ of my 2 parents, the child is immediately
deleted, too.
***
This is my first attempt to use delete-orphan. I don't dare to report
it as a bug,
for the case I misunderstand the SA API.
The complete code:
from sqlalchemy import *
class Child(object):
def __init__(self, name):
self.childname = name
class Parent(object):
def __init__(self, name):
self.parentname = name
db = create_engine("mysql://[EMAIL PROTECTED]/test_cascade")
db.echo = True
session = create_session()
metadata = BoundMetaData(db)
t_parent = Table("parent",metadata,
Column("id",Integer,primary_key=True),
Column("parentname",String()),
Column("child_id",Integer,ForeignKey("child.id")),
mysql_engine="InnoDB",
)
t_child = Table("child",metadata,
Column("id",Integer,primary_key=True),
Column("childname",String()),
mysql_engine="InnoDB",
)
metadata.create_all()
mapper(Child, t_child)
mapper(Parent, t_parent, properties={
"mychild":relation(Child,cascade="delete-orphan"),
})
# create a child + 2 parents:
aChild = Child("aChild");session.save(aChild)
aParent1 = Parent("aParent1"); aParent1.mychild=aChild;
session.save(aParent1)
aParent2 = Parent("aParent2"); aParent2.mychild=aChild;
session.save(aParent2)
session.flush()
# it doesn't matter whether I create a new session here, or continue
with the old one
# delete first parent:
session.delete(aParent1)
# With InnoDB, the next flush raises Foreign Key constraint failure
# because aChild is deleted - while aParent2 ist still referencing it !
session.flush() #fails
# with MyISAM, I can still go ahead ...
print session.query(Child).get_by(childname="aChild") # the child is
deleted
# ... and try to delete second parent:
session.delete(aParent2) #
# next flush raises an error because aChild is not attached to session
anymore
session.flush()
It this a Bug, or did I misuse the ORM... ?
Regards
Ruben
-----
Versions: sqlalchemy 0.3.3 and 0.3.1 and MySQL5.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---