Let me try that again, with the attachment this time...
import sys
sys.path.append("/Users/dmiller/Code/PyOE/resources/lib/SQLAlchemy/lib")

import sqlalchemy.attributes as attributes

# relationship:
#
#  Blog -- (contains many) --> Post -- (contains many) --> Comment
#


manager = attributes.AttributeManager()

class Post(object):pass
class Blog(object):pass
class Comment(object):pass

# orphan rules
delete_orphans = {
    Post : [('posts', Blog)],  # a Post must be in a 'posts' collection on Blog, or its an orphan
    Blog : [],
    Comment : [('comments', Post)]  # a Comment must be in a 'comments' collection on a Post, or its an orphan
}

# check the "hasparent" property of all of an object's orphan rules to determine orphan status
def _is_orphan(obj):
    d = delete_orphans[obj.__class__]
    for (key,klass) in d:
        if not getattr(klass, key).hasparent(obj):
            return True
    else:
        return False

# set up instrumented attributes with backrefs    
manager.register_attribute(Post, 'blog', uselist=False, extension=attributes.GenericBackrefExtension('posts'), trackparent=True)
manager.register_attribute(Blog, 'posts', uselist=True, extension=attributes.GenericBackrefExtension('blog'), trackparent=True)
manager.register_attribute(Post, 'comments', uselist=True, extension=attributes.GenericBackrefExtension('post'), trackparent=True)
manager.register_attribute(Comment, 'post', uselist=False, extension=attributes.GenericBackrefExtension('comments'), trackparent=True)

# print out the integer ids of all the InstrumentedAttribute objects
print "Blog.posts", id(Blog.posts)
print "Post.blog", id(Post.blog)
print "Post.comments", id(Post.comments)
print "Comment.post", id(Comment.post)

# create objects as if they'd been freshly loaded from the database (without history)
b = Blog()
p1 = Post()
Blog.posts.set_callable(b, lambda:[p1])
Post.blog.set_callable(p1, lambda:b)

# now add a new comment
c1 = Comment()
p1.comments.append(c1)

# assert connections
assert p1.blog is b
assert p1 in b.posts
assert c1.post is p1
assert c1 in p1.comments

print "Blog:", b._state
print "Post", p1._state
print "Comment", c1._state

# no orphans
assert not _is_orphan(c1)
assert not _is_orphan(p1) # fails!
assert not _is_orphan(b)

On Sep 1, 2006, at 10:21 AM, dmiller wrote:


i think you understand this already; the way hasparent works is:

        if a class "A" has a property "bs", managed by
InstrumentedAttribute "Bs" that points to instances of class "B", then
        "B" is an orphan if its _state does not contain a record
"('hasparent',id(Bs))".


Right, that's what I thought should happen. Now continue your example
so C has a property "b" managed by an InstrumentedAttribte "CB" that
points to an instance of class B. In my case the instance of class
"B" is getting a "('hasparent',id(CB))" record in its _state.

Ahh, it looks like the same thing is happening in your test program,
so that's not causing my problem (and I guess it's not a bug either).
Why does that happen though? Here's another example:

For reference, here's a snippet of the output of your test program:

Blog.posts 6092976
Post.blog 6071408
...
Blog: {('hasparent', 6071408): True, 'modified': True}
Post {('hasparent', 6206960): True, ('hasparent', 6092976): True,
'modified': True}

Why does b._state have a 'hasparent' record for p1.blog? That would
imply that "p1.blog" is the parent of "b", which doesn't seem logical
to me. This is exactly what was happening in my program (and I
thought it was a bug).

However, this does give me a better understanding of the
InstrumentedAttribute parent tracking mechanism though, so thanks for
that. It allowed me to reproduce the scenario that is happening
within my program, and now I have a test that fails (see attached).

~ Daniel



---------------------------------------------------------------------- --- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel? cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to