On 03/02/2010 20:25, Michael Bayer wrote:
werner wrote:
In my model I have:

class Country(BaseExt):
      pass

sao.mapper(Country, createSelect(Country_D, Country_T, 'countries_d_id',
['name', 'url']))

Which I can use like this:

for cs in session.query(db.Country).all():
      print cs.name, cs.id

But I run into problems when I try to use "Country" in a relation like
this:

class Region_D(Base, CreateUpdateMixin):
      __tablename__ = u'regions_d'

      id = sa.Column(sa.Integer(), sa.Sequence('regions_d_id'),
primary_key=True, nullable=False)
      name = sa.Column(sa.String(length=50, convert_unicode=False))

      countries_d_id = sa.Column(sa.Integer())

      country = sao.relation('Country', backref='region_d',
primaryjoin='Region_D.countries_d_id == Country.id')

I am getting this exception also "Country" is defined before "Region_D":
if you use "'Country'" as a string in relation(), the declarative base
looks for it inside of Base._decl_class_registry.  Its not here since
Country isn't part of Base.   You should be saying "Country", i.e. send
the actual class, to the relation().
Thanks, just tried this but I get the same exception. Then changed "Country" to use "Base" and but then I get this exception.

Traceback (most recent call last):
  File "saTest.py", line 5, in <module>
    import model as db
  File "D:\Dev\aaTests\sqla\i18nAlt2\model.py", line 223, in <module>
    class Country(Base):
  File "D:\Dev\aaTests\sqla\i18nAlt2\model.py", line 105, in __init__
    return DeclarativeMeta.__init__(cls, classname, bases, dict_)
File "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\ext\declarative.py", line 561, in __init__
    _as_declarative(cls, classname, dict_)
File "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\ext\declarative.py", line 516, in _as_declarative "specified and does not inherit from an existing table-mapped class." % cls) sqlalchemy.exc.InvalidRequestError: Class <class 'model.Country'> does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class.

I am trying a "property" approach and that seems to do the trick.

I.e.:
class Region_D(Base, CreateUpdateMixin):
    __tablename__ = u'regions_d'

id = sa.Column(sa.Integer(), sa.Sequence('regions_d_id'), primary_key=True, nullable=False)
    name = sa.Column(sa.String(length=50, convert_unicode=False))

countries_d_id = sa.Column(sa.Integer(), sa.ForeignKey(u'countries_d.id'))

    country_d = sao.relation('Country_D', backref='region_d')

    country = TranslationPropertyAlt('Country', 'countries_d_id', 'id')

class TranslationPropertyAlt(object):
    def __init__(self, tTable, fkCol):
        self.tTable = tTable
        self.fkCol = fkCol

    def __get__(self, obj, objtype):
        tTable = globals()[self.tTable]
        fKey = getattr(obj, self.fkCol)
        try:
return sao.object_session(obj).query(tTable).filter_by(id=fKey).one()
        except sao.exc.NoResultFound:
            print 'no translation was found'
            print 'tTable: %s' % (self.tTable)
            traceback.print_exc()

Need to do more testing and clean up the code a lot - but I think there is a very small light at the end of the tunnel.

Thanks again for all the tips and help
Werner


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

Reply via email to