Re: one-to-one association, howto

Wara Songkran <[email protected]> Fri, 18 Feb 2005 01:19:54 +0700
Newsgroups gmane.comp.python.modeling
Message-ID <[email protected]>
On 17 Feb 2005 12:34:43 +0100, Sebastien Bigaret
<[email protected]> wrote:
>=20
>        Hi,
>=20
> Wara Songkran <[email protected]> wrote:
> > I've try to create one-to-one association from table Task to Supplier b=
y define
> >
> > model.association =3D [
> >   Association('Task', 'Supplier',multiplicity=3D[ [0, 1], [0, 1] ])
> > ]
> >
> > but the error said
> >
> > ValueError: invalid multiplicity dst->src: should be toMany
> >
> > I've also apply the patch from
> > http://sourceforge.net/mailarchive/message.php?msg_id=3D9401573
> >
> > Did I done something wrong?
>=20
> The problem is that one-to-one relationships are still not supported
> out of the box, at least not the way one think they should be :/
>=20
> For the moment one-to-one rels. should be modeled as one-to-many
> (http://modeling.sourceforge.net/UserGuide/design-rels.html)
>=20
> Back to your example, your model becomes::
>=20
>  model.association =3D [
>    ## this is in fact a one-to-one modeled as a one-to-many,
>    ## class 'Supplier' defines getTask() and setTask()
>    Association('Task', 'Supplier',multiplicity=3D[ [0, 1], [0, None] ],
>                relations=3D['supplier', 'tasks'] )
>  ]
>=20
> and then you add to the class Supplier the following code::
>=20
>   def validateTasks(self, aValue):
>     """
>     ``tasks`` is a to-one relationship modeled as a to-many (inverse of
>     the to-one rel ``Task.supplier``): it is valid iff it has no more
>     than one element
>=20
>     :exception Modeling.Validation.ValidationException: if more than one
>       task is registered
>     :see: `getTask()`, `setTask()`
>     """
>     # see http://modeling.sf.net/UserGuide/customobject-validation-by-key=
.html
>     # subsection 3.3.2.2.
>     from Modeling import Validation
>     if len(self.getTasks())>1:
>       raise Validation.ValidationException
>     return
>=20
>  def getTask(self):
>    if self.getTasks():
>        return self.getTasks()[0]
>    return None
>=20
>  def setTask(self, task):
>    if self.getTask():
>      self.removeFromTasks(self.getTask())
>    self.addToTasks(task)
>=20
>  # you can also define the corresponding property:
>  task =3D property(getTask, setTask, None, "to-one relationship to Task")
>=20
> Additional note: with this definition you'll have a foreign key in entity
>  'Task' pointing to the primary key in entity 'Supplier'.  It seems
>  reasonable, but in case you want the inverse, swap tasks and supplier in=
 the
>  association's definition and define get/setSupplier() in class Task.
>=20
>        Regards,
>=20
> -- S=E9bastien.
>=20
>=20

Thank for your reseponse. Now I know more limitation of Modeling. I'm
developing a relational <--> xml mapping (relational <--> object <-->
xml) using your framework and EaseXML http://xmlobject.base-art.net/
(nothing serius just a part of my school project)
Do you have any suggestion.

Regard
Wara Songkran


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click