add optional base parameter to SqlSoup.entity to override a base class for mapped objects

Issue #1975 resolved
Former user created an issue

this is useful when you want to create some helper functions/properties.

discussed on http://bit.ly/c77Dt2

here's the final patch:

==================== --- sqlsoup.py.old 2010-11-17 22:41:14.212129086 +0000 +++ sqlsoup.py 2010-11-18 01:50:13.443881826 +0000 @@ -540,7 +540,8 @@ j = join(args, *kwargs) return self.map(j) - def entity(self, attr, schema=None): + def entity(self, attr, schema=None, base=None): + if base and attr in self._cache: del self._cacheattr try: t = self._cacheattr except KeyError, ke: @@ -548,7 +549,7 @@ if not table.primary_key.columns: raise PKNotFoundError('table %r does not have a primary key def ined %s' % (attr, ','.join(table.c.keys()))) if table.columns: - t = _class_for_table(self.session, self.engine, table, self.base) + t = _class_for_table(self.session, self.engine, table, base or self.base) else: t = None self._cacheattr = t ====================

Comments (5)

  1. Mike Bayer repo owner

    as I've mentioned in email, I don't like this solution at all. its fragile and inelegant. So far I'm still of the opinion that SqlSoup is not intended for ad-hoc creation of real domain models and you should be using declarative instead. What's the reason for not doing so ?

  2. Mike Bayer repo owner

    a new method map_to() has been added to SqlSoup in c848624f9db325213ddf2fde9819946f9eb235cd. This is a method that you can call ahead of time, given a particular SqlSoup attribute name, to map that name to a class/table/selectable in a specific way. Subsequent calls to that name via regular __getattr__() access will return that mapped class. The method is strict and can only be called once for a particular name, and only for names that do not exist already, meaning you're going to want to call a series of map_to() calls right when you generate your SqlSoup object:

    db = SqlSoup('mysql://...')
    db.map_to('users', tablename='users', base=MyUserBase)
    db.map_to('addresses', tablename='addresses', base=MyAddressBase)
    
    # use normally
    
    for user in db.users.all():
       print user
    
  3. Log in to comment