Thanks Michael, I greatly appreciate your help. But I am still a bit 
confused. To set the attribute I need to have the alias for MPTT (or, 
actually any class that inherits MPTT mixin class) table. But I can not use 
aliased() neither inside the MPTT definition nor after it — in both cases I 
get the mapping error. 

On Wednesday, August 1, 2012 3:46:56 AM UTC+2, Michael Bayer wrote:
>
>
> On Jul 31, 2012, at 11:42 AM, Sergey Kucheryavski wrote:
>
> I would like to use Mixin to make a base class for MPTT trees, just as an 
> example
>
> class MPTT(object):
>     @declared_attr
>     def __tablename__(cls):
>         return cls.__name__.lower()
>
>     id = db.Column(db.Integer, primary_key = True)
>     level = db.Column(db.Integer, nullable = False)
>     lft = db.Column(db.Integer, nullable = False)
>     rgt = db.Column(db.Integer, nullable = False)
>
>     @declared_attr
>     def parent_id(cls):
>         return db.Column(db.Integer, db.ForeignKey(cls.id))
>
>     @declared_attr
>     def num_children(cls):
>         mptt2 = aliased(cls)
>         return 
> column_property(select([func.count(cls.id)]).where(cls.parent_id 
> == mptt2.id))
>
>
>
> yeah num_children() is evaluated within declarative's metaclass, before 
> MPTT is mapped.  So you have to set that attribute on after the fact:
>
> mptt2 = aliased(cls)
> MPTT.num_children = column_property(....)
>
> docs (see the last example):
>
>
> http://docs.sqlalchemy.org/en/rel_0_7/orm/mapper_config.html#using-column-property
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/3tDxuZQ9VbwJ.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to