On Mon, Sep 1, 2008 at 4:06 PM, Alex Marandon <[EMAIL PROTECTED]> wrote: > > Hello there, > > I use Elixir to map an existing database which has tables with a lot > of columns. Mapping all these columns when building Elixir entities > takes time and it actually becomes a real performance problem when > using my entities for batch processing. > > To address this issue, I can pass a list of fields I want to > SQLAlchemy like this: > > session.query(Contact.id, Contact.fullName).first() > > it's very efficient but it gives me RowTuple objects on which I can't > call the methods and properties I've defined on my entity class. > > I could also put my methods and properties in a mixin class and define > a different entity for each set of fields I want to load: > > class ContactMixin(object): > @property > def pretty_name(self): > return "***" + self.fullName + "***" > > class FullContact(Entity, ContactMixin): > using_options(tablename='contacts', autoload=True) > > class LightWeightContact(Entity, ContactMixin): > using_options(tablename='contacts', autoload=True) > using_mapper_options(include_properties=['id', 'fullName']) > > setup_all() > > contact = FullContact.query.first() > print contact.pretty_name > print contact.any_other_field > > contact = LightWeightContact.query.first() > print contact.pretty_name > try: > print contact.any_other_field > except AttributeError, e: > print 'We expect to raise here:', e.message > > That works well until I want to add field aliases. If I want to add a > field to my base class, Elixir doesn't do its magic on it:
You can't add columns in an autoloaded entity, so far. I never thought about this precise use case of only adding aliases to existing columns. Could you add a ticket in Elixir trac to this effect? thanks... > class ContactMixin(object): > full_name = Field(Text, colname='fullName') > @property > def pretty_name(self): > return "***" + self.fullName + "***" > (...) And you can't add columns in a Mixin... I've never >>> print contact.full_name > Property(None, None) # Does not return the value from the database > > Another way I haven't really explored yet is using the defer() option, > which apparently won't exactly work out of the box as I wish because > it requires to specify the list of fields I don't want to load, and I > would rather need an inclusion syntax, similar to include_properties, > that would differ all the fields but the ones I specify (my tables > really have a lot of fields). I can of course write some kind of > wrappers building the list of deferred properties but before > reinventing the wheel, I thought I'd ask here if there is any > recommended way to tackle this problem. When querying, I'd like to be > able to specify a subset of fields to load, ideally on a > query-by-query basis, and get full-featured elixir entities back. If you go down the deferred path, you might want to look into deferred groups. To be honest, if you only use autoloaded tables, you'd probably have more luck with SQLAlchemy's built-in declarative extension. -- Gaƫtan de Menten http://openhex.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "SQLElixir" 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/sqlelixir?hl=en -~----------~----~----~----~------~----~------~--~---
