Re: Why no 1to1 relationships?
Re: Why no 1to1 relationships?
- Subject: Re: Why no 1to1 relationships?
- From: Art Isbell <email@hidden>
- Date: Wed, 2 Apr 2008 09:02:01 -1000
On Apr 2, 2008, at 8:45 AM, Robert Walker wrote:
Yes, it is possible to create one-to-one relationships, joined on
the primary keys. One side of the relationship will need "Propagates
primary key," as has been mentioned.
I just wanted to add one caveat to this configuration: The
relationship will always be 1-to-1 and will never be 1-to-0. In
other words if you created an instance of the entity on the side
propagating the key, you will get an instance of the other side of
the relationship. This will happen even if no fields are set on the
other side. This means that if any attributes (other than the PK) on
the other side of the relationship are set to required, neither
object will save.
Also if you attempt to create a new instance on other other side
(not propagating the key) you will also have problems saving.
As far as I recall this is the experience that I've had when
attempting to do this. Because of these complications I now opt for
the more flexible one-to-one style by setting it up in the model as
a one-to-many relationship. Then force the many-side to allow only
one or zero related rows though validation logic.
Another way of stating this is that a one-to-one relationship must be
mandatory (i.e., it cannot be optional). This makes sense because the
foreign key in a one-to-one relationship is the primary key which
cannot be null and is guaranteed to be unique. If a foreign key is
not null, the destination object must exist. If a foreign key is not
the primary key, it is not guaranteed to be unique, so an attribute
that is not the primary key cannot be the foreign key of a one-to-one
relationship.
I think that this agrees with relational database theory; i.e., all
one-to-one relationships must be 1:1, not 1:0. A 1:0 relationship is
just a special case of a 1:many relationship.
Aloha,
Art
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden