Re: Managed Object wants a per-relationship attribute?
Re: Managed Object wants a per-relationship attribute?
- Subject: Re: Managed Object wants a per-relationship attribute?
- From: mmalc crawford <email@hidden>
- Date: Sat, 15 Sep 2007 18:50:43 -0700
On Sep 15, 2007, at 6:35 PM, Jerry Krinock wrote:
Say that I'm modelling a swimming meet. My data model has two
entities: Events and Swimmers. Of course, each Event requires
multiple Swimmers, and each Swimmer can swim in several Events.
My Events should show swimmers ordered by speed: 1st, 2nd, 3rd,
etc. Since CoreData's to-many relationships are not ordered, I add
to Swimmer a 'position' attribute. In the 100-meter freestyle
Event, Suzie's position is 1 and Jane's position is 2. But in some
other event, say the 200-meter butterfly, Jane is faster than Suzie,
so the positions need to be reversed. But I can't do that since
each Swimmer only has one 'position' attribute.
[...]
Duh! What's the proper way to model this?
Summary: You need to make the join table explicit and add an attribute
to that.
In a traditional relational database, the many-to-many relationship
would be expressed using a join table (an intermediate entity).
Event <-->> SwimmerEvent <<-> Swimmer
Core Data hides this from you. This usually makes life easier. If
you need to add additional information to the intermediate entity, you
need to make it explicit and add the attribute yourself.
So create a new entity (SwimmerEvent or somesuch), break the direct
relationships between Event and Swimmer, and create relationships to
SwimmerEvent instead.
Event
swimmerEvents ->> SwimmerEvent
(inverse is event)
Swimmer
swimmerEvents ->> SwimmerEvent
(inverse is swimmer)
SwimmerEvent
event -> Event
swimmer -> Swimmer
position (Integer)
mmalc
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden