问题
I have an entity A which has two attributes. Entity B has A as parent and has an additional 3 attributes. The changes in the new version don't affect entities A and B.
How can I migrate objects of entity B to a new version of my data model, including the attributes from entity A?
I tried using two entity mappings: one for A and one for B, but 'A attributes' aren't migrated. Alternatively I would add A's attributes to the mapping to migrate B, but there I can't selected the right attributes (in Xcode 4).
Edit:
I'm not referring to a regular relationship between two entities, but inheritance:
Edit 2:
Just to be sure, I created a new project to test with. Herein, I added only the two entities as seen above. In my awakeFromNib
I do a fetch request and if no results are returned, I add a new entity:
NSManagedObject *newAccount = [[NSManagedObject alloc] initWithEntity:entityDesc insertIntoManagedObjectContext:[self managedObjectContext]];
// Account
[newAccount setValue:@"TheName" forKey:@"name"];
[newAccount setValue:[NSDecimalNumber decimalNumberWithMantissa:5 exponent:2 isNegative:NO] forKey:@"currentBalance"];
// BankDebitAccount
[newAccount setValue:@"TheAccountNumber" forKey:@"accountNumber"];
[newAccount setValue:@"TheBankName" forKey:@"bankName"];
[newAccount setValue:[NSDecimalNumber decimalNumberWithMantissa:6 exponent:1 isNegative:YES] forKey:@"openingBalance"];
In my second version of the data model, I added a new entity and I enabled automatic migration via
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:dict error:&error]) {
The migration does indeed happen, and the three properties from BankDebitAccount successfully are migrated. The currentBalance property from Account is reset to 0 and the name property isn't visible in the XML file anymore (and thus, is equal to nil).
Edit 3: I just tried opening this newly made test project in Xcode 3(.2.4). When I open the mapping model in there and select my child entity's mapping, I can actually add an attribute mapping for the parent's attributes:
So, I guess that would make this a bug in Xcode 4.
回答1:
I am not sure what went wrong.
I just created this data model version 1, I hope this gets as close to your case as possible:
Then I created this version 2, no changes to Parent and Child only one new entity:
The I created a mapping model and this is what it automatically suggested:
Let's have a look at the differences:
Only one change: the new entity Neighbour.
Can you post some pics of your situation?
Note This is XCode3
回答2:
Turns out this was a bug in the version of Xcode I was using at the time and was resolved in Xcode 4.2.
来源:https://stackoverflow.com/questions/6129539/migrating-entities-and-parent-entities