Re: Question: Entities and Primary Keys and "Primary Keys"
Giorgio Valoti <[email protected]>
| Newsgroups | gmane.comp.web.webobjects.eof |
|---|---|
| Message-ID | <[email protected]> |
On Lunedì, feb 24, 2003, at 08:05 Europe/Rome, Ashley Aitken wrote: > Howdy All, > > I read somewhere that is it wise, for example with category codes, to > have the primary key (a database construct) plus a unique code for each > category as a part of the object model (I will call that the entity > primary key). > > This enables objects that refer to the category to use the unique > category code (entity primary key) as opposed to the database primary > key and thus to survive restoration of the category code table (ie > where > the primary keys may change but the category codes will stay the same) > from backup etc. > > I have two questions: > > 1. This would seem to apply to all entities and thus we should not use > the database primary key as the primary way of distinguishing entities > in relationships etc. Hence we should have the database primary key > and > a separate entity primary key (which will become part of the object > model but should be set uniquely once and not changed) for each entity. > We should use the entity primary key for foreign keys and relationship > joins etc. > > 2. If this is the case, I guess we have to set up an index for this > entity primary key manually (as I don't believe EOModeller will do it). > > Of course, this question would not arise if we use the entity "primary > key" as the database primary key (if you know what I mean) but we are > generally advised not to do this (because having the real primary key > as > a part of the object model leaves it open to erroneous changes etc). imho, is much more open to erroneous changes if you use this approach. Also consider that you have to apply the correct constraints to two attibutes, i.e. the 'database primary key' and the 'entity primary key'. However, if you use "synthetic" primary keys (say, generated from sequences) it's true that is generally wise to avoid binding your logic with key values because, by definition, they don't have any meaning. HTH Giorgio Valoti ------------------------------------------------------ Magnetic Media Network: <http://www.magneticmedia.com> ------------------------------------------------------ Public folder <http://homepage.mac.com/giorgio_v/FileSharing1.html> ------------------------------------------------------