@@ -716,11 +716,12 @@ - (id)newValueForRelationship:(NSRelationshipDescription *)relationship
716716
717717 if (![relationship isToMany ]) {
718718 // to-one relationship, foreign key exists in source entity table
719-
719+ BOOL shouldFetchSourceEntityType = [self entityNeedsEntityTypeColumn: sourceEntity];
720+
720721 NSString *string = [NSString stringWithFormat:
721722 @" SELECT %@%@ FROM %@ WHERE __objectID=?" ,
722723 [self foreignKeyColumnForRelationship: relationship],
723- shouldFetchDestinationEntityType ? @" , __entityType" : @" " ,
724+ shouldFetchSourceEntityType ? @" , __entityType" : @" " ,
724725 [self tableNameForEntity: sourceEntity]];
725726 statement = [self preparedStatementForQuery: string];
726727 sqlite3_bind_int64 (statement, 1 , key);
@@ -1459,18 +1460,18 @@ - (BOOL)migrateFromModel:(NSManagedObjectModel *)fromModel toModel:(NSManagedObj
14591460 case NSAddEntityMappingType: {
14601461 NSString *destRootEntityName = [self rootForEntity: destinationEntity].name ;
14611462 NSEntityDescription *destRootEntity = destinationEntities[destRootEntityName];
1462- if ([updatedRootEntities containsObject: destRootEntity]) {
1463- return ;
1464- }
1465- [updatedRootEntities addObject: destRootEntity];
1463+ if (![updatedRootEntities containsObject: destRootEntity]) {
1464+ [updatedRootEntities addObject: destRootEntity];
14661465
1467- NSString *srcRootEntityName = destRootEntity.name ;
1468- NSEntityDescription *srcRootEntity = [sourceEntities objectForKey: srcRootEntityName];
1466+ NSString *srcRootEntityName = destRootEntity.name ;
1467+ NSEntityDescription *srcRootEntity = [sourceEntities objectForKey: srcRootEntityName];
1468+
1469+ success &= [self alterTableForSourceEntity: srcRootEntity
1470+ destinationEntity: destinationEntity
1471+ withMapping: nil
1472+ error: error];
1473+ }
14691474
1470- success &= [self alterTableForSourceEntity: srcRootEntity
1471- destinationEntity: destinationEntity
1472- withMapping: nil
1473- error: error];
14741475 [destinationEntity.directRelationshipsByName enumerateKeysAndObjectsUsingBlock: ^(NSString * _Nonnull key, NSRelationshipDescription * _Nonnull obj, BOOL * _Nonnull relationshipStop) {
14751476 NSString *tableName = [self tableNameForRelationship: obj];
14761477 BOOL hasTable;
@@ -1496,15 +1497,15 @@ - (BOOL)migrateFromModel:(NSManagedObjectModel *)fromModel toModel:(NSManagedObj
14961497
14971498 NSString *destRootEntityName = srcRootEntity.name ;
14981499 NSEntityDescription *destRootEntity = [destinationEntities objectForKey: destRootEntityName];
1499- if ([updatedRootEntities containsObject: destRootEntity]) {
1500- return ;
1500+ if (![updatedRootEntities containsObject: destRootEntity]) {
1501+ [updatedRootEntities addObject: destRootEntity];
1502+
1503+ success &= [self alterTableForSourceEntity: srcRootEntity
1504+ destinationEntity: destRootEntity
1505+ withMapping: nil
1506+ error: error];
15011507 }
1502- [updatedRootEntities addObject: destRootEntity];
15031508
1504- success &= [self alterTableForSourceEntity: srcRootEntity
1505- destinationEntity: destRootEntity
1506- withMapping: nil
1507- error: error];
15081509 // TODO: should we remove many-to-many relationship tables here?
15091510 } break ;
15101511
@@ -1514,15 +1515,15 @@ - (BOOL)migrateFromModel:(NSManagedObjectModel *)fromModel toModel:(NSManagedObj
15141515 NSString *destRootEntityName = [self rootForEntity: destinationEntity].name ;
15151516 NSEntityDescription *srcRootEntity = sourceEntities[srcRootEntityName];
15161517 NSEntityDescription *destRootEntity = destinationEntities[destRootEntityName];
1517- if ([updatedRootEntities containsObject: destRootEntity]) {
1518- return ;
1518+ if (![updatedRootEntities containsObject: destRootEntity]) {
1519+ [updatedRootEntities addObject: destRootEntity];
1520+
1521+ success &= [self alterTableForSourceEntity: srcRootEntity
1522+ destinationEntity: destRootEntity
1523+ withMapping: nil
1524+ error: error];
15191525 }
1520- [updatedRootEntities addObject: destRootEntity];
15211526
1522- success &= [self alterTableForSourceEntity: srcRootEntity
1523- destinationEntity: destRootEntity
1524- withMapping: nil
1525- error: error];
15261527 if (success) {
15271528 success &= [self alterRelationshipForSourceEntity: sourceEntity
15281529 destinationEntity: destinationEntity
0 commit comments