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(). > > Traceback (most recent call last): > File "saTest.py", line 41, in <module> > for cs in session.query(db.Country).all(): > File > "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\orm\session.py", > line 893, in query > return self._query_cls(entities, self, **kwargs) > File > "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\orm\query.py", > line 92, in __init__ > self._set_entities(entities) > File > "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\orm\query.py", > line 101, in _set_entities > self._setup_aliasizers(self._entities) > File > "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\orm\query.py", > line 115, in _setup_aliasizers > mapper, selectable, is_aliased_class = _entity_info(entity) > File > "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\orm\util.py", > line 492, in _entity_info > mapper = class_mapper(entity, compile) > File > "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\orm\util.py", > line 567, in class_mapper > mapper = mapper.compile() > File > "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\orm\mapper.py", > line 687, in compile > mapper._post_configure_properties() > File > "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\orm\mapper.py", > line 716, in _post_configure_properties > prop.init() > File > "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\orm\interfaces.py", > line 408, in init > self.do_init() > File > "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\orm\properties.py", > line 714, in do_init > self._get_target() > File > "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\orm\properties.py", > line 731, in _get_target > self.mapper = mapper.class_mapper(self.argument(), compile=False) > File > "c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\ext\declarative.py", > line 624, in return_cls > prop.parent, arg, n.args[0], cls)) > sqlalchemy.exc.InvalidRequestError: When compiling mapper > Mapper|Region_D|regions_d, expression 'Country' failed to locate a name > ("name 'Country' is not defined"). If this is a class name, consider > adding this relation() to the <class 'model.Region_D'> class after both > dependent classes have been defined. > > Am I doing something wrong or is it not possible to use a class/mapper > based on a select in a relation? > > Werner > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to sqlalch...@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. > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@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.