On Jul 7, 2011, at 3:29 PM, James Studdart wrote:

> Hi all,
> I've got a question regarding hybrid properties and how to use them with 
> single table inheritance.
> 
> I've got a class hierarchy like this (in semi-pseudo code):
> 
> class MyBase(object):
>    # This has the tablename declared attr, id as primary key, generic table 
> args etc.
> 
> class Person(MyBase, Base):
>    children = relationship('Children')
> 
> class SpecialPerson(Person):
>    partner = relationship('Person')
> 
> Okay, so what I want is for SpecialPerson to return both it's own plus it's 
> partners children. But, if I add to list of children of a special person, it 
> only adds to it's local children list. Does that make sense?
> 
> This is what I've got now, I'm stabbing around in the dark a little bit, so 
> I'm hoping for some guidance in the correct  way to do this with SQL Alchemy.

What you have is fine, though it is only functional at the instance level, that 
is:

p = Session.query(SpecialPerson).first()
print p.children

At the class level, the __add__() operator doesn't have any behavior for 
relationship() and also cls.partner is again a relationship() which doesn't 
have a _children attribute.   That is, this wouldn't work:

print 
Session.query(SpecialPerson).filter(SpecialPerson.chlidren.contains(some_person))

Assuming you don't need that (class level behavior), you don't really need 
@hybrid_property either.   You can just use Python's standard @property.

If you *did* want that, it would be a little tricky, probably would need a 
custom comparator.


-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to