[ZODB-Dev] Objects containing references to other objects

Chris McDonough chrism@digicool.com
Tue, 05 Jun 2001 08:39:43 -0400


Jeff Kowalczyk wrote:
> 
> If you have an object (say "person") who might have other objects of
> various classes (other person, appointment, purchaseorder, todo, etc.)
> that should be linked to them in some way, what does a ZDB user
> typically do?
> 
> Modeling the objects as children/collections won't work for me. Most of
> these other objects would be first-class objects that have application
> features elsewhere in the system, and other locations.

References to objects can be kept in more than one place.  For example,
a reference to an appointment could be kept on a person object and also
in a list of appointments:

lunch = Appointment('12:00', 'Eat lunch')
person.appointments = lunch
appointments = (lunch,)

> For example, a purchase order system might be a standalone applet, but
> you still want the 'company' field to point to an object in your
> separate contact database of company-class objects. Likewise, you'd want
> persons to point to their company object, if they have one.
> 
> Aside from app structure, the secondary point of this line of
> questioning is to determine what and ODB/ZDB user does to prevent
> objects from having (possibly misspelled) text strings for names of
> objects that live elsewhere in the system. A string for your person or
> company name in the Purchase order is not as useful as the true
> ObjectID.

I'd just store have a reference if I were able.

Fred Drake's weak references (in Python 2.X) might help solve the
"object hangs along for longer than it should" problem you might end up
having if you use references.  I've not used weakrefs with ZODB, I'm not
sure if they work.

> I guess its still a holdover from my RDBMS mindset, but I'm not ready to
> give up normalization with ID's when I take the plunge into an ODBMS
> like zope. Do I have to, or does storing objectID's work? Does this
> explode into a complexity of object lifetimes that takes all the fun and
> practicality out of using Zope?

It's often the case that people get in to such a relational frame of
mind that it's almost impossible to dig them out.  :-)

I'm not sure what "normalization of ids" means above.  If it means "does
every object have an autogenerated id", the answer is yes.  But the
further answer might be "don't use it", because it's not guaranteed
reliable or to not change between ZODB releases.

Zope uses string ids with some success (because every object needs to be
addressed directly by a user... if this weren't the case, it's likely
that none of the objects would have ids).  You can experiment with
weakrefs.  Or maybe just try to model the problem space with aggregation
to see what happens, I imagine a solution may fall out.

- C