Hi,

I've encountered a couple of instances of a specific SQL design  
pattern, aimed at mimicking a linked list (of comments and sub- 
comments to blog entries) and I'd just like to check that the pattern  
is known not to be supported in ActiveMapper.

Michael Bayer describes the pattern as "handling a hierarchical  
relationship on itself", and in his implementation, "contains a lazy  
reference both to its parent comment and its list of child comments."

1st instance is reflog's Turboblog, uses SQLObject and handles the  
mapping explicitly:

class Comment(SQLObject):
     class sqlmeta:
         table = "turboblog_comment"
     [...]
     parent_id = IntCol(default=-1)
     sub_comments = RelatedJoin("Comment",joinColumn="parent_id")

2nd instance is Michael Bayer's zblog, uses SQLAlchemy, mapping is  
handled via foreign_key:

comments = Table('comments', metadata,
     Column('comment_id', Integer, primary_key=True),
     [...]
     Column('parent_comment_id', Integer, ForeignKey 
('comments.comment_id')),
     )

augmented with:

# comment mapper.  This mapper is handling a hierarchical  
relationship on itself, and contains
# a lazy reference both to its parent comment and its list of child  
comments.
mapper(Comment, tables.comments, properties={
     'id':tables.comments.c.comment_id,
     'post':relation(Post, lazy=True, backref=backref('comments',  
cascade="all, delete-orphan")),
     'user':relation(user.User, lazy=False, backref=backref 
('comments', cascade="all, delete-orphan")),
     'parent':relation(Comment,  
primaryjoin=tables.comments.c.parent_comment_id==tables.comments.c.comme 
nt_id, foreignkey=tables.comments.c.comment_id, lazy=True,  
uselist=False),

I've tried both approaches in ActiveMapper, neither of which seem to  
work (but I'm a newbie w.r.t sqlalchemy):

class Comment(ActiveMapper):
     class mapping:
         __table__= "comment"
         comment_id    = column(Integer, primary_key=True)
         [...]
         parent_id = column(Integer, default=-1)
         sub_comments  = one_to_many("Comment", colname="parent_id",  
backref="comment_id")

ArgumentError: Cant find any foreign key relationships between  
'comment' and 'comment'

class Comment(ActiveMapper):
     class mapping:
         __table__= "comment"
         comment_id    = column(Integer, primary_key=True)
         [...]
         parent_id = column(Integer,  
foreign_key="comment.comment_id", index=True)
         sub_comments  = one_to_many("Comment", colname="parent_id",  
backref="comment_id")

AttributeError: 'ColumnProperty' object has no attribute  
'_get_target_class'

I'm thinking that this design pattern may be outside the intentions  
of ActiveMapper --- Michael Bayer has already indicated that  
"ActiveMapper is just giving you a thin layer of "shortcut" on top of  
things".

(It's not that it can't be done with an explicit Table+mapper, it's  
just that I'm lazy and prefer to work at the highest level of  
abstraction available if possible.)

Cheers,

Graham Higgins.





-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to