Hi everyone:

I have an object structure that uses (heavily) object orientation.

I have a "BaseObject" class that is the class for three kinds of
subclasses, "Element1", "Element2" and "Element3". In my application I
have a tree of "BaseObject" elements:

class BaseObject(declarative):
        __tablename__ = "base_objects"

        _polymorphicIdentity = Column("polymorphic_identity", String(20),

key="polymorphicIdentity")
        __mapper_args__ = {
                'polymorphic_on': _polymorphicIdentity,
                'polymorphic_identity': None
        }
        _id = Column("id", Integer, primary_key=True, key="id")
        _parentId = Column("parent_id", Integer,
                                      ForeignKey("base_objects.id"),
key="parentId")
        _children = relationship("BaseObject",
                                                         collection_class=set,
                                                         
backref=backref("_parent", remote_side=lambda: BaseObject.id,

             uselist=False),
                                                         
#order_by=??????????????
                                                         )

class Element1(BaseObject):
        __tablename__ = "elements_1"
        _id = Column("id", Integer, ForeignKey(BaseObject.BaseObject.id),
                             primary_key=True)
        
        __mapper_args__ = {
                'polymorphic_identity': 'Element1',
                'inherit_condition': _id == BaseObject.BaseObject._id,
        }
        _name = Column("name", String(50))

class Element2(BaseObject):
        __tablename__ = "elements_2"
        _id = Column("id", Integer, ForeignKey(BaseObject.BaseObject.id),
                             primary_key=True)
        
        __mapper_args__ = {
                'polymorphic_identity': 'Element2',
                'inherit_condition': _id == BaseObject.BaseObject._id,
        }
        _name = Column("name", String(50))

class Element3(BaseObject):
        __tablename__ = "elements_3"
        _id = Column("id", Integer, ForeignKey(BaseObject.BaseObject.id),
                              primary_key=True)
        
        __mapper_args__ = {
                'polymorphic_identity': 'Element3',
                'inherit_condition': _id == BaseObject.BaseObject._id,
        }

As you can see, only two of those classes have a "_name" attribute.

What I'd like to know is if I can set an "order_by" in the
BaseObject._children relationship that does the following:
1) Sort by type, but not the default ordering. If I set up (in
_children) the order_by to order by _polymorphicIdentity (by the
discriminator), I'd get instances of type "Element1", then instances
of "Element2" and finally, from "Element3". I don't want that. I need
to get "Element1", "Element3" and finally, "Element2".
2) Sort by name if the child has a name. If it doesn't, leave it with
the 'special' _polymorphicIdentity described above if the child class
doesn't have a _name attribute.

I don't even know if that's possible.
Because of certain dependencies, I can't use SqlAlchemy 0.7.x yet. I
am using 0.6.8

Thank you very much in advance!

-- 
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