>> Hi all, this is my first post to this list. >> >> Finally I was convinced to migrate from sqlobject to sqlalchemy but >> haven't dived into sqlalchemy yet, just contemplating how my migration >> should go. I guess db/class definition will be straightforward, >> converting most of the queries too, but there are a couple of things I >> could locate in my sqlobject code that might need special treatment. >> I'd very much appreciate some comments on these with a view towards a >> sqlalchemy n00b like myself. I mean best practices, general philosophy >> and the like :) >> >> 1. >> >> I have a number of tables/objects and these all share some common >> fields/properties. For example, there are different types of objects >> but all can be commented on, so all tables of these objects should >> have a comment_id field and all corresponding classes should have a >> comment_count( ), last_comment( ) method. In sqlobject I could solve >> this by ordinary python inheritance, I defined a mixin with the right >> properties/methods and any object that needed comments subclassed not >> only SQLObject but also this mixin. In this way I didn't need to add >> these fields/methods/properties to each table definition. How would I >> do something similar in sqlalchemy? > > its interesting that works with SQLObject.
I thought so too, it's pretty tricky! But it works. > Our own declarative > extension provides a syntax that is very much like SQLObject and I'd > recommend its usage, but as of yet it doesn't support inheritance in > quite that straightforward of a way, meaning ORM-mapped fields placed > on the declared class have to be defined distinctly each time. > > But, you can achieve the effect of establishing each field just once > by subclassing the declarative metaclass. This looks like: > > class MyMeta(DeclarativeMeta): > def __init__(cls, classname, bases, dict_): > dict_['comment_id'] = Column(Integer) > return DeclarativeMeta.__init__(cls, classname, bases, dict_) > > your regular methods go on a base class: > > class MyBase(object): > def comment_count(self): > pass > > then you set up declarative via: > > Base = declarative_base(cls=MyBase, metaclass=MyMeta) > > Its a little less convenient to make the metaclass but we might try to > improve upon that in a future release. I can see declarative pulling > up the Column objects from a superclass that has no __table__ and > copying them for each class. Thanks a lot, I'll try going with this solution. >> In sqlobject I had a query which is simple in SQL but complicated >> through sqlobject. It was the following: let's say there is a zoo, >> which has some cages, each cage has a couple of animals in it, and >> each animal has a number of legs. All of this is stored in a database >> in an obvious way. How do I select the total number of legs in a zoo >> using one query with sqlalchemy? > > Assuming you had a chain of relationships set up it would be like: > > session.query(func.count(Leg.id)).join((Animal, Leg.animals), (Cage, > Animal.cage), (Zoo, Cage.zoo)).filter(Zoo.id==<some zoo id>).scalar() Thanks for this tip too. Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---
