[Zope-dev] PROPOSAL: ZODB Relationships

Lennart Regebro lennart@regebro.nu
Fri, 09 May 2003 11:53:47 +0200


Jean Jordaan wrote:

> Isn't this naming the relationship?
> 
>             student_courses = Relationship()
> 
> So you can examine the relationship you are interested in by finding
> it in the repository for relationships (eg. in a folder) as:
> 
>   <relationshipfolder>.student_courses
>   <relationshipfolder>.course_teachers

Well, the above code doesn't create that attribute...
It would have to be:

    <relationshipfolder>.student_courses = Relationship()

And that kills off the idea of a global storage (which I don't mind at 
all. I don't really see the use of having several relationship storages, 
but I'm easily convinced otherwise if somebody can come up with a good 
use case).

The above code also is bad because it allows anybody to kill all stored 
relations by just creating a new Relations object and assigning that to 
the object attribute. It's then better to have an API more like

	<storage>.addRelation(relid, objecta, objectb)

The relationship with id relid would then be created if it didn't exist. 
  Or you could create the relationship separately, but still by using a 
method call that check that it doesn't overwrite and kills an existing 
relationship. Like addIndex() on a catalog.

This would mean that you have code more like this:

class course:
     def __init__(self):
         relstorage = GetRelationshipStorage()
         relstorage.CreateRelationship('students')

     def assignStudent(self, student):
         relstorage = GetRelationshipStorage()
         relstorage.addRelationship('students', self, student)

That last part could also be:

    relstorage.students.addRelationship(self, student)

but I don't like that, really. I could go for

    relstorage['students'].addRelationship(self, student)

though.