1 Aug 2012 16:20
Re: aliased in mixins
Michael Bayer <mike_mp <at> zzzcomputing.com>
2012-08-01 14:20:23 GMT
2012-08-01 14:20:23 GMT
oh, sorry, that's a mixin. this combination of variables is not supported at this time. You need to use an event:
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlalchemy <at> googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
from sqlalchemy import event
from sqlalchemy.orm import mapper
<at> event.listens_for(mapper, "mapper_configured")
def set_num_children(mapper, cls):
if not issubclass(cls, MPTT):
return
mptt2 = aliased(cls)
cls.num_children = column_property(
select([func.count(cls.id)]).where(cls.parent_id == mptt2.id))
On Aug 1, 2012, at 7:20 AM, Sergey Kucheryavski wrote:
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 exampleclass MPTT(object):<at> declared_attrdef __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)<at> declared_attrdef parent_id(cls):return db.Column(db.Integer, db.ForeignKey(cls.id))<at> declared_attrdef num_children(cls):mptt2 = aliased(cls)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):--
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 <at> googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlalchemy <at> googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
RSS Feed