Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow import of entities without a primary key (relatedByAttribute) #641

Merged
merged 3 commits into from

3 participants

@SergioEstevao

#Issue 640

If a primary key is not defined always do an insert.

Sérgio Este... added some commits
@tonyarnold
Owner

Thanks Sérgio, this looks good — would you mind adding some tests to verify the behaviour of this change?

@tonyarnold tonyarnold was assigned
@SergioEstevao
@tonyarnold
Owner

Hi Sérgio, just push your changes up to your develop branch. This pull request should be updated (and rechecked) automatically. Thanks!

@SergioEstevao
@tonyarnold
Owner

Looks good Sérgio, thanks! Give me a couple of days to check and merge this — it's the weekend here, and I'm out of the house.

@SergioEstevao

Any news about this pull request?

@paolonl paolonl referenced this pull request from a commit in paolonl/MagicalRecord
Paolo Moroni Added possibility to importFromObject without a primaryAttribute bein…
…g set (magicalpanda#641)
372af28
@tonyarnold tonyarnold merged commit 81f27a0 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 3, 2014
  1. Make import always create new entities when no primaryAttribute is de…

    Sérgio Estêvão authored
    …fined.
Commits on Jan 6, 2014
  1. Fix code format.

    Sérgio Estêvão authored
Commits on Jan 10, 2014
  1. Add tests for import without a primary key.

    Sérgio Estêvão authored
This page is out of date. Refresh to see the latest.
View
12 MagicalRecord.xcodeproj/project.pbxproj
@@ -7,6 +7,10 @@
objects = {
/* Begin PBXBuildFile section */
+ 581ECBF7187F660B00084FEE /* MultipleEntitiesWithNoPrimaryKey.json in Resources */ = {isa = PBXBuildFile; fileRef = 581ECBF6187F660B00084FEE /* MultipleEntitiesWithNoPrimaryKey.json */; };
+ 581ECBF8187F660B00084FEE /* MultipleEntitiesWithNoPrimaryKey.json in Resources */ = {isa = PBXBuildFile; fileRef = 581ECBF6187F660B00084FEE /* MultipleEntitiesWithNoPrimaryKey.json */; };
+ 581ECBF9187F663100084FEE /* ImportMultipleEntitiesWithNoPrimaryKeyTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 581ECBEB187F63FF00084FEE /* ImportMultipleEntitiesWithNoPrimaryKeyTests.m */; };
+ 581ECBFA187F663200084FEE /* ImportMultipleEntitiesWithNoPrimaryKeyTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 581ECBEB187F63FF00084FEE /* ImportMultipleEntitiesWithNoPrimaryKeyTests.m */; };
90171E1617C3210F00E7084A /* NSManagedObjectContext+MagicalSavesSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 9099490017C2F0B700BC2B5C /* NSManagedObjectContext+MagicalSavesSpec.m */; };
90171E1717C3210F00E7084A /* NSManagedObjectContext+MagicalSavesSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 9099490017C2F0B700BC2B5C /* NSManagedObjectContext+MagicalSavesSpec.m */; };
90171E1B17C329CC00E7084A /* FixtureHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = C7CF9B6C17498B5C008D9D13 /* FixtureHelpers.m */; };
@@ -273,6 +277,8 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
+ 581ECBEB187F63FF00084FEE /* ImportMultipleEntitiesWithNoPrimaryKeyTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImportMultipleEntitiesWithNoPrimaryKeyTests.m; sourceTree = "<group>"; };
+ 581ECBF6187F660B00084FEE /* MultipleEntitiesWithNoPrimaryKey.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = MultipleEntitiesWithNoPrimaryKey.json; path = Fixtures/MultipleEntitiesWithNoPrimaryKey.json; sourceTree = "<group>"; };
90425375187102DC0066DA41 /* MagicalRecord-iOS-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MagicalRecord-iOS-Prefix.pch"; sourceTree = "<group>"; };
90425376187102DC0066DA41 /* MagicalRecord-OSX-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MagicalRecord-OSX-Prefix.pch"; sourceTree = "<group>"; };
90425377187103060066DA41 /* MagicalRecordVersion.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MagicalRecordVersion.h; sourceTree = "<group>"; };
@@ -681,6 +687,7 @@
C7CF9B4E17498985008D9D13 /* SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.json */,
C7CF9B4F17498985008D9D13 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.json */,
C7CF9B5017498985008D9D13 /* SingleEntityWithNoRelationships.json */,
+ 581ECBF6187F660B00084FEE /* MultipleEntitiesWithNoPrimaryKey.json */,
C7CF9B5117498985008D9D13 /* SingleEntityWithNoRelationships.plist */,
C7CF9B5217498985008D9D13 /* SingleRelatedEntity.json */,
);
@@ -711,6 +718,7 @@
C7CF9789174982AD008D9D13 /* ImportSingleEntityRelatedToMappedEntityWithNestedMappedAttributesTests.m */,
C7CF978A174982AD008D9D13 /* ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m */,
C7CF978B174982AD008D9D13 /* ImportSingleEntityWithNoRelationshipsTests.m */,
+ 581ECBEB187F63FF00084FEE /* ImportMultipleEntitiesWithNoPrimaryKeyTests.m */,
90BB1C3E1864F662001BBFBB /* ImportSingleRelatedEntityTests.m */,
C7CF978D174982AD008D9D13 /* MagicalDataImportTestCase.h */,
C7CF978E174982AD008D9D13 /* MagicalDataImportTestCase.m */,
@@ -1019,6 +1027,7 @@
C7CF9B5317498985008D9D13 /* SampleJSONDataForImport.json in Resources */,
C7CF9B5517498985008D9D13 /* SingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeys.json in Resources */,
C7CF9B5717498985008D9D13 /* SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.json in Resources */,
+ 581ECBF7187F660B00084FEE /* MultipleEntitiesWithNoPrimaryKey.json in Resources */,
C7CF9B5917498985008D9D13 /* SingleEntityRelatedToMappedEntityUsingDefaults.json in Resources */,
C7CF9B5B17498985008D9D13 /* SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey.json in Resources */,
C7CF9B5D17498985008D9D13 /* SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.json in Resources */,
@@ -1037,6 +1046,7 @@
C7CF9B5417498985008D9D13 /* SampleJSONDataForImport.json in Resources */,
C7CF9B5617498985008D9D13 /* SingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeys.json in Resources */,
C7CF9B5817498985008D9D13 /* SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.json in Resources */,
+ 581ECBF8187F660B00084FEE /* MultipleEntitiesWithNoPrimaryKey.json in Resources */,
C7CF9B5A17498985008D9D13 /* SingleEntityRelatedToMappedEntityUsingDefaults.json in Resources */,
C7CF9B5C17498985008D9D13 /* SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey.json in Resources */,
C7CF9B5E17498985008D9D13 /* SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.json in Resources */,
@@ -1171,6 +1181,7 @@
90171E1617C3210F00E7084A /* NSManagedObjectContext+MagicalSavesSpec.m in Sources */,
9099495F17C2F42100BC2B5C /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.m in Sources */,
90BB1C3A1864F341001BBFBB /* ImportSingleEntityRelatedToMappedEntityUsingDefaultsTests.m in Sources */,
+ 581ECBF9187F663100084FEE /* ImportMultipleEntitiesWithNoPrimaryKeyTests.m in Sources */,
90542E1A1864688100916224 /* ImportSingleEntityRelatedToMappedEntityWithNestedMappedAttributesTests.m in Sources */,
90542E2618648AAF00916224 /* NSPersistentStoreCoordinatorHelperTests.m in Sources */,
90542E201864855500916224 /* NSManagedObjectContextHelperTests.m in Sources */,
@@ -1218,6 +1229,7 @@
90171E1717C3210F00E7084A /* NSManagedObjectContext+MagicalSavesSpec.m in Sources */,
9099496017C2F42100BC2B5C /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.m in Sources */,
90BB1C3B1864F341001BBFBB /* ImportSingleEntityRelatedToMappedEntityUsingDefaultsTests.m in Sources */,
+ 581ECBFA187F663200084FEE /* ImportMultipleEntitiesWithNoPrimaryKeyTests.m in Sources */,
90542E1B1864688100916224 /* ImportSingleEntityRelatedToMappedEntityWithNestedMappedAttributesTests.m in Sources */,
90542E2718648AB000916224 /* NSPersistentStoreCoordinatorHelperTests.m in Sources */,
90542E211864855500916224 /* NSManagedObjectContextHelperTests.m in Sources */,
View
8 MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalDataImport.m
@@ -286,8 +286,12 @@ + (id) MR_importFromObject:(id)objectData inContext:(NSManagedObjectContext *)co
id value = [objectData MR_valueForAttribute:primaryAttribute];
- NSManagedObject *managedObject = [self MR_findFirstByAttribute:[primaryAttribute name] withValue:value inContext:context];
- if (managedObject == nil)
+ NSManagedObject *managedObject = nil;
+ if (primaryAttribute != nil)
+ {
+ managedObject = [self MR_findFirstByAttribute:[primaryAttribute name] withValue:value inContext:context];
+ }
+ if (managedObject == nil)
{
managedObject = [self MR_createInContext:context];
}
View
40 Tests/DataImport/ImportMultipleEntitiesWithNoPrimaryKeyTests.m
@@ -0,0 +1,40 @@
+//
+// ImportMultipleEntitiesWithNoPrimaryKeyTests.m
+// MagicalRecord
+//
+// Created by Sérgio Estêvão on 09/01/2014.
+// Copyright (c) 2014 Magical Panda Software LLC. All rights reserved.
+//
+
+#import "MagicalDataImportTestCase.h"
+#import <XCTest/XCTest.h>
+#import "FixtureHelpers.h"
+#import "SingleEntityWithNoRelationships.h"
+
+@interface ImportMultipleEntitiesWithNoPrimaryKeyTests : MagicalDataImportTestCase
+
+@property (nonatomic, retain) NSArray * arrayOfTestEntity;
+
+@end
+
+@implementation ImportMultipleEntitiesWithNoPrimaryKeyTests
+
+- (void)setUp
+{
+ [super setUp];
+
+ self.arrayOfTestEntity = [SingleEntityWithNoRelationships MR_importFromArray:self.testEntityData];
+}
+
+- (void)tearDown
+{
+ [super tearDown];
+}
+
+- (void)testImportOfMultipleEntities
+{
+ XCTAssertNotNil(self.arrayOfTestEntity, @"arrayOfTestEntity should not be nil");
+ XCTAssertEqual(self.arrayOfTestEntity.count, (NSUInteger)4, @"arrayOfTestEntity should have 4 entities");
+}
+
+@end
View
14 Tests/Fixtures/MultipleEntitiesWithNoPrimaryKey.json
@@ -0,0 +1,14 @@
+[
+ {
+ "stringTestAttribute": "This is a test value",
+ },
+ {
+ "stringTestAttribute": "This is a test value",
+ },
+ {
+ "stringTestAttribute": "This is a test value",
+ },
+ {
+ "stringTestAttribute": "This is a test value",
+ }
+]
Something went wrong with that request. Please try again.