• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: preventing duplicate records: how to?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: preventing duplicate records: how to?


  • Subject: Re: preventing duplicate records: how to?
  • From: Chuck Hill <email@hidden>
  • Date: Tue, 27 May 2003 09:17:18 -0700

Hi Tom,

You approach will work provided that you never run more than one instance
and don't dispatch requests concurrently.  Otherwise you have a race
condition that has the potential to result in duplicate records.  In
situations like this I find it best to rely on the database as the point
for consistency checks.

To do this you would create a unique constraint on Person for these two
attributes.  It is important to make this a named constraint, don't let the
DB use a system generated name.  Then wrap saveChanges() in a try...catch
block.  You will have to check and parse the exception for the name you
used for the unique constraint.  Ugly and clumsy?  Yes.  But it absolutely
will prevent duplicates.  You can make an EC sublcass that overrides
saveChanges() and does most of this.  It would then raise a custom
exception (e.g. DuplicateNameException).


Chuck


At 12:01 PM 27/05/2003 -0400, Tom Woteki wrote:
>I am seeking an alternative to an approach I have for preventing the
>insertion or update of records that duplicate selected fields of
>already existing records. My approach is based on comparing
>EOGlobalIDs. I keep thinking I have missed something obvious or that
>there may be preferred way of doing this.
>
>Here is an example of my situation: Say I have an entity, Person, whose
>primary key does not involve any of N other attributes, 2 of which are
>firstName and surname. I want to prevent the insertion of new Persons
>or updating of existing Persons that would result in duplicates of
>these 2 attributes. (Note: These are not the only 2 attributes for
>which I want to avoid duplicates, so simply making their combination
>the primary key would not solve my problem.)
>
>Here is the approach I use: Say p is a new Person entity I am about to
>insert (i.e. saveChanges() after insertObject() has been called on some
>editingContext). I fetch any entities whose 2 attributes from above
>match those of p. If I find any whose EOGlobalID in the editing context
>is different than p's I conclude I am about to create a duplicate and
>prevent the save. I use a similar technique for updates.
>
>Is there another way, a preferred way, of doing this?
>
>Thanks
>Tom
>_______________________________________________
>webobjects-dev mailing list | email@hidden
>Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
>Do not post admin requests to the list. They will be ignored.
>

--

Chuck Hill                                 email@hidden
Global Village Consulting Inc.             http://www.global-village.net
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >preventing duplicate records: how to? (From: Tom Woteki <email@hidden>)

  • Prev by Date: preventing duplicate records: how to?
  • Next by Date: Re: MS SQL Server2000 EOModel Connection Problem
  • Previous by thread: preventing duplicate records: how to?
  • Next by thread: Re: preventing duplicate records: how to?
  • Index(es):
    • Date
    • Thread