title: Validation Errors with Core Data Migration
body: <p>I'm encountering a very annoying error using Core Data on Mac OS X 10.5.</p>
  
  <p>All entities have classes in my application which share the same names.</p>
  
  <p>Up to this point, I've done 5 migrations without problems.</p>
  
  <p>I'm using the XML data store.
  I've got V5 of my data model with 13 entities, 4 of which are abstract. It looks like this:
  <a href="http://synapticmishap.co.uk/ObjectModel.jpg" rel="nofollow noreferrer">alt text http://synapticmishap.co.uk/ObjectModel.jpg</a></p>
  
  <p><strong>What I do</strong></p>
  
  <ol>
  <li><p>Create a new version of my data
  model - this is V6.</p></li>
  <li><p>Add a new entity (JGToolbarWindow)
  and set the parent to JGWindowBase.</p></li>
  <li><p>Added a mapping model that goes from
  V5 to V6. Saved it.</p></li>
  <li><p>Set the current version to be V6.</p></li>
  <li><p>Build and Debug.</p></li>
  </ol>
  
  <p>I've got automatic migration switched on in the Persistent Store Coordinator options dictionary.</p>
  
  <p><strong>The Problem</strong></p>
  
  <ol>
  <li><p>Get an error - "Multiple validation
  errors occurred."</p></li>
  <li><p>I've set a breakpoint and examined
  the validation errors. There's a long
  list of validation errors - there
  seems to be one for every data entry.</p></li>
  <li><p>It seems that it's stripping out ALL
  the relationships and since one of my
  properties - application - is set to
  be required, this counts as a
  validation error.</p></li>
  <li><p>So it seems the problem lies with the
  migration not being able to migrate
  the relationships... even though
  apparently nothing has changed as far
  as relationships go.</p></li>
  <li><p>When I look in ~/Library/Application
  Support/Name of my app/ I see an XML
  Data Store that's got the same name
  appended by .xml.new When looking at
  this file in a text editor, all the
  relationships have indeed been
  stripped out.</p></li>
  </ol>
  
  <p><strong>What I've tried</strong></p>
  
  <ol>
  <li><p>Creating a new XML data store and
  putting a handful of items in it,
  then trying again. I get the same
  errors for each item.</p></li>
  <li><p>Cleaning and rebuilding.</p></li>
  <li><p>Making sure the old .xml.new from
  previous validation has been
  deleted.</p></li>
  <li><p>Deleting the new model version and
  repeating my actions to make sure I
  didn't screw something up.</p></li>
  <li><p>Adding mapping entries for each of
  the abstract classes, which are
  ignored by default. Added all
  attributes and relationships. Still
  exactly the same error.</p></li>
  <li><p>Tried adding a new entity that
  doesn't have a class, just based on
  NSManagedObject which has
  JGWindowBase as a parent. Again,
  failed.</p></li>
  </ol>
  
  <p>The only thing I can think that has changed is some of my internal logic in the classes underlying the model. But these are minor changes and I can't understand how it was migrating fine, and now it's not when nothing appears to have changed.</p>
  
  <p>I've tried a brand new migration from a renamed latest data store and have exactly the same problems. Which means I've got to solve this problem for <strong>any</strong> data migration to work.</p>
  
  <p><strong>Errors</strong></p>
  
  <p>When I put a breakpoint at the presentError line below</p>
  
  <pre><code>if (![persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType
                                                    configuration:nil 
                                                              URL:url 
                                                          options:options 
                                                            error:&amp;error]){
            [[NSApplication sharedApplication] presentError:error];
  }
  </code></pre>
  
  <p>and examine the error userInfo, I get lots of errors like this:</p>
  
  <pre><code>Error Domain=NSCocoaErrorDomain Code=1570 UserInfo=0x67ebfd0 "item is a required value."
  </code></pre>
  
  <p><code>po (NSArray *)0x67ebfd0</code></p>
  
  <p>gives:</p>
  
  <pre><code>NSLocalizedDescription = "item is a required value.";
  NSValidationErrorKey = item;
  NSValidationErrorObject = &lt;NSManagedObject: 0x45d5830&gt; (entity: JGLogEntry; id: 0x4baccb0 &lt;x-coredata://B597E13E-BE74-402E-BF00-5E1D57898406/JGLogEntry/p13942&gt; ; data: {
  duration = nil;
  item = nil;                   // &lt;&lt;&lt; Here's the problem.
  processBundleID = nil;
  processID = nil;
  processName = nil;
  startTime = 2009-10-09 16:41:44 +0100;
  stopTime = 2009-10-09 16:42:17 +0100;
  windowDocumentPathOrURL = nil;
  windowID = nil;
  windowTitle = nil;
  </code></pre>
  
  <p>All this really tells me is that it's removed the relationship connected to item, which is a required property which is why it's telling me there are validation errors. There are a whole load for application too, which is the other required property.</p>
  
  <p>As I see it, the problem isn't with the validation errors as such, it's that it seems to be not migrating <strong>any relationships at all</strong>.</p>
  
  <p>I'm pulling my hair out with this. I'd really, really appreciate some help. And finally, a Star Wars quote:</p>
  
  <blockquote>
    <p>"Help me, Stack Overflow. You're my
    only hope."</p>
  </blockquote>