Suppose we have a 1 <-> N relation between users and cities (meaning that
every user is related with one city). For our domain model "User" we want
to define a method "lives_in_london". Code looks like this:
class User(Base):
id = ...
city_id = ...
city = relationship("City")
def lives_in_london(self):
return self.city.name == 'London'
class City(Base):
id = ...
name = Column(String)
The problem with this code is that "lives_in_london" method is a leaky
abstraction, its client must keep in mind that it may issue a DB query.
It's okay as a one-off thing, but will case problems if used in a loop.
So to be used efficiently, clients must know to preload the "city"
relationship.
I know it's a contrived example, but the general question is how to define
domain methods that need to access relations. I also know that the question
is intrinsic to all ORM, but maybe SQLAlchemy could offer some support.
I would appreciate any ideas.
--
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.