Thanks, I got it working. For future reference, if you have a
polymorphic relationship, in order to avoid this kind of phantom object,
you need to manually delete from both the parent collection and the
child collection, e.g.

Project (Has Many)-> Node
Parameter (Subclass)-> Node

to remove a parameter from a project, you need to do both

project.parameters.remove(param)
project.nodes.remove(param)

-Dave

-----Original Message-----
From: sqlalchemy@googlegroups.com [mailto:[EMAIL PROTECTED]
On Behalf Of Michael Bayer
Sent: Thursday, October 26, 2006 5:43 PM
To: sqlalchemy@googlegroups.com
Subject: [sqlalchemy] Re: related object list not being updated on
delete


items dont get removed from collections when they are marked with  
delete().  with the exception of bi-directional relationships, SA  
doesnt add or remove items from collections.  to do so in this case  
would require tracking every collection which a particular item is  
contained within which would add both memory and performance overhead.

the usual strategy is to mark the relationship with a "delete-orphan"  
cascade, and then just remove the item from the collection, which  
will result in its getting deleted during the flush() operation.

On Oct 26, 2006, at 5:13 PM, Hogarty, David A. wrote:

>
> I'm having a problem where phantom child objects stay attached to a
> parent object after having been deleted. For example:
>
> def test_add_delete_parameter(self):
>     p = Project(name='ptest')
>     db.session.save(p)
>     db.session.flush()
>     p.parameters.append(Parameter(name='p1'))
>     p.parameters.append(Parameter(name='p2'))
>     p.parameters.append(Parameter(name='p3'))
>     db.session.flush()
>     id = p.parameters[-1].id
>     param = db.session.query(Parameter).get_by(
>         and_(
>             Parameter.c.project_id == p.id,
>             Parameter.c.id == id
>             ))
>     db.session.delete(param)
>     db.session.flush()
>     p2 = db.session.query(Project).get(p.id)
>     assert len(p2.parameters) == 2
>     assert len(p.parameters) == 2
>
> Both of the assertions at the bottom fail, because the project  
> instance
> still believes it has 3 parameters attached. Is this a bug, or  
> should I
> be deleting the parameters differently?
>
> -Dave
>
> >




--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to