Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Coredata schema question.



On May 31, 2005, at 9:09 PM, James Andrews wrote:
I am getting back into my project as I was waiting for CD to be released. I am trying to come up with a good schema, I am use to SQL schema for web apps, but I am not sure how to handle certain aspects with CD. First off, this link is an image of what I have so far for my schema.

http://www.vmdi.net/images/PhotoBox/coredata.gif

First off, you should name your entities in the singular rather than the plural — as if you were naming classes or objects in the real world , not database tables. Thus rather than a Categories entity and a Photographs entity, you would want a Category entity and a Photograph entity.


You also appear to be inverting some of your relationships. That is, your relationship Category.albums appears to be a to-one relationship from Category to Albums. You probably want that to be a to-many relationship, just as your relationship Albus.categories is. (And of course adjust the tenses as above — to-one relationships should be singular, to-many relationships should be plural.) I suspect you may be thinking in terms of foreign keys and joins rather than in terms of relationships between objects.

My first question. I have a Categories Entity. I want to be able to have categories that are children of categories. I am not sure how to build a relationship for this. Suggestions on how to do it, and explanations on why you would do it that way would greatly appreciated.

You can create a reflexive relationship easily. Just create "parent" and "children" relationships in Category and set the destination entity of each to Category. Make Category.parent a to-one relationship and make Category.children a to-many, and make one the inverse of the other. Doing this models a hierarchy that you can traverse in either direction.


I want to be able to link Photographs to Albums, and want to be able to put the same image in multiple Albums. I had thought maybe another Entity between Albums and Photographs, but I am not sure if that would work the way I would expect it too. Suggestions on how to do it, and explanations on why you would do it that way would greatly appreciated.

That's how you would do it in a database — you would use a many-to- many correlation table. However, with Core Data you're working with entities and relationships so you can simply declare that your Photograph.albums relationship and Album.photographs relationship are both to-many and are each others' inverses. Core Data will take care of the rest.


  -- Chris

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Coredata schema question. (From: James Andrews <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.