> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of werner
> Sent: 04 February 2010 09:41
> To: [email protected]
> Subject: Re: [sqlalchemy] Using a arbitrary select
> mapper/class in a relation - is this allowed?
>
> 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.
Just to confirm, were you actually defining your Region_D class exactly
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=countries_d_id == Country.id)
Ie. Neither the class nor the primaryjoin parameters in the relation
were strings? I would be surprised if you got the exception you
described (expression 'Country' failed to locate a name ) if you had
done that, because SA wouldn't be trying to look up that name.
Simon
--
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.