@@ -414,6 +414,7 @@ - (NSIncrementalStoreNode *)newValuesForObjectWithID:(NSManagedObjectID *)object
414414
415415 // enumerate properties
416416 NSDictionary *properties = [entity propertiesByName ];
417+ __block NSUInteger tableAliasIndex = 0 ;
417418 [properties enumerateKeysAndObjectsUsingBlock: ^(NSString *key, NSPropertyDescription *obj, BOOL *stop) {
418419 if ([obj isKindOfClass: [NSAttributeDescription class ]]) {
419420 [columns addObject: [NSString stringWithFormat: @" %@ .%@ " , table, key]];
@@ -434,18 +435,15 @@ - (NSIncrementalStoreNode *)newValuesForObjectWithID:(NSManagedObjectID *)object
434435 if ([self entityNeedsEntityTypeColumn: destinationEntity]) {
435436 // Get the destination table for the type look up
436437 NSString *destinationTable = [self tableNameForEntity: destinationEntity];
437- NSString *destinationAlias = [NSString stringWithFormat: @" T%lu l" ,(unsigned long )destinationEntity.hash];
438+ NSString *destinationAlias = [NSString stringWithFormat: @" t%lu " , (unsigned long )tableAliasIndex];
439+ tableAliasIndex++;
438440
439441 // Add teh type column to the query
440442 NSString *typeColumn = [NSString stringWithFormat: @" %@ .__entityType" , destinationAlias];
441443 [columns addObject: typeColumn];
442444
443- NSString *join = [NSString stringWithFormat: @" INNER JOIN %@ as %@ ON %@ .__objectid=%@ .%@ " , destinationTable, destinationAlias, destinationAlias, table, column];
444-
445- // this part handles optional relationship
446- if (relationship.optional ) {
447- join = [join stringByAppendingFormat: @" OR %@ .%@ IS NULL" , table, column];
448- }
445+ // Use LEFT JOIN as a relationship can be optional
446+ NSString *join = [NSString stringWithFormat: @" LEFT JOIN %@ as %@ ON %@ .__objectid=%@ .%@ " , destinationTable, destinationAlias, destinationAlias, table, column];
449447
450448 [typeJoins addObject: join];
451449
0 commit comments