On 7/2/14, 9:24 AM, Chung WONG wrote:
> Hi list,
>
> For this problem I am even having trouble think of a proper subject
> for it.
> I try my best to express as clear as possible and sorry for any
> confusions.
>
> Say there are three classes with relationship defined as below:
> class User(Base):
> __tablename__ = 'users'
> id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
>
> class Feedback(Base):
> __tablename__ = 'feedbacks'
> id = Column(Integer, Sequence('feedback_id_seq'), primary_key=True)
> service_id = Column(Integer, ForeignKey('services.id'))
> score=Column(Integer)
>
> service=relationship('Service', backref=backref("feedbacks"))
>
> class Service(Base):
> __tablename__ = 'services'
> id = Column(Integer, Sequence('service_id_seq'), primary_key=True)
> provider_id = Column(Integer, ForeignKey('users.id'))
> requester_id = Column(Integer, ForeignKey('users.id'))
>
> provider = relationship('User', foreign_keys=provider_id,
> backref='services_received')
> requester = relationship('User', foreign_keys=requester_id,
> backref='services_sent')
>
>
> *User* and *Service* is a one to many relationship, and a user can be
> a Service provider or requester.
>
> For *Service* and *Feedback, *it is a one to many relationship too. A
> requester can give a score to the Service.
>
> *The question is, how can I get the sum(scores) of all services for a
> user?*
> *
> *
> I thought I could do sth like:
> #provider is a given object
> *provider.services_recieved.feedbacks*
> but it threw error:
> *AttributeError: 'InstrumentedList' object has no attribute 'feedbacks'*
there's no query(x.y.z.bar) feature in the SQLAlchemy ORM. To join
across entities, use join(), this is first introduced in the ORM
tutorial at
http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#querying-with-joins.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.