Uploaded image for project: 'Realm Cocoa SDK'
  1. Realm Cocoa SDK
  2. RCOCOA-725

[realm-core-6.0.6] Assertion failed: primary_key_col

      Goals

      I am trying to migrate objects from my Draft table to BDraft and CDraft tables, depending on the type of object.

      Expected Results

      I have already made a similar migration which worked well. So I expect found my objects in the expected tables after migration

      Actual Results

      The app crashes when trying to create the object with primary key during migration

      Steps to Reproduce

      <pre>
      <code>
      /Users/realm/workspace/realm_realm-core_release_6.0.6/src/realm/table.cpp:2427: [realm-core-6.0.6] Assertion failed: primary_key_col
      0 Realm 0x00000001066f9e24 ZN5realm4utilL18terminate_internalERNSt3_118basic_stringstreamIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE + 28
      1 Realm 0x00000001066fa0c0 _ZN5realm4util9terminateEPKcS2_lOSt16initializer_listINS0_9PrintableEE + 324
      2 Realm 0x00000001066c476c _ZN5realm5Table30create_object_with_primary_keyERKNS_5MixedE + 832
      3 Realm 0x00000001062ae894 ZN5realm6Object6createIU8strongP11objc_object18RLMAccessorContextEES0_RT0_RKNSt3_110shared_ptrINS_5RealmEEERKNS_12ObjectSchemaET_NS_12CreatePolicyENS_6ObjKeyEPNS_3ObjE + 2112
      4 Realm 0x00000001062af3a4 RLMCreateObjectInRealmWithValue + 1192
      5 Realm 0x0000000106380b84 -[RLMRealm createObject:withValue:] + 92
      6 Realm 0x0000000106296c08 -[RLMMigration createObject:withValue:] + 96
      7 MyApp 0x00000001009a0618 $s8 MyApp 17DBHelperMigrationC03setD0yySo12RLMMigrationC_s6UInt64VtFZySo9RLMObjectCSg_AKtXEfU0_ + 3984
      8 MyApp 0x000000010099f5f8 $sSo9RLMObjectCSgACIggg_A2CIeggg_TR + 20
      9 MyApp 0x000000010099f660 $sSo9RLMObjectCSgACIeggg_A2CIyByy_TR + 92
      10 Realm 0x0000000106296474 -[RLMMigration enumerateObjects:block:] + 1764
      11 MyApp 0x000000010099f22c $s8 MyApp 17DBHelperMigrationC03setD0yySo12RLMMigrationC_s6UInt64VtFZ + 5340
      12 MyApp 0x0000000100da2f98 $s8 MyApp 7DBToolsC16setGeneralConfig33_4FC9A2C58AEF85141A5A6F637BEA26CBLLySo21RLMRealmConfigurationCAGFZySo12RLMMigrationC_s6UInt64VtcfU_ + 148
      13 MyApp 0x0000000100da3004 $sSo12RLMMigrationCs6UInt64VIeggy_AbDIeyByy_TR + 92
      14 Realm 0x0000000106296a74 -[RLMMigration execute:] + 1124
      15 Realm 0x000000010639298c ZZ47+[RLMRealm realmWithConfiguration:queue:error:]ENK3$_2clENSt3_110shared_ptrIN5realm5RealmEEES4_RNS2_6SchemaE + 400
      16 Realm 0x0000000106392794 ZNSt31L8invokeIRZ47+[RLMRealm realmWithConfiguration:queue:error:]E3$_2JNS_10shared_ptrIN5realm5RealmEEES6_RNS4_6SchemaEEEEDTclclsr3std31E7forwardIT_Efp_Espclsr3std31E7forwardIT0_Efp0_EEEOS9_DpOSA + 144
      17 Realm 0x00000001063926d0 ZNSt3128invoke_void_return_wrapperIvE6callIJRZ47+[RLMRealm realmWithConfiguration:queue:error:]E3$_2NS_10shared_ptrIN5realm5RealmEEES8_RNS6_6SchemaEEEEvDpOT + 112
      18 Realm 0x000000010639262c ZNSt3110function12alloc_funcIZ47+[RLMRealm realmWithConfiguration:queue:error:]E3$_2NS_9allocatorIS2_EEFvNS_10shared_ptrIN5realm5RealmEEES8_RNS6_6SchemaEEEclEOS8_SD_SA + 112
      19 Realm 0x0000000106390cc0 ZNSt3110function6funcIZ47+[RLMRealm realmWithConfiguration:queue:error:]E3$_2NS_9allocatorIS2_EEFvNS_10shared_ptrIN5realm5RealmEEES8_RNS6_6SchemaEEEclEOS8_SD_SA + 112
      20 Realm 0x0000000106440c1c ZNKSt3110function12value_funcIFvNS_10shared_ptrIN5realm5RealmEEES5_RNS3_6SchemaEEEclEOS5_SA_S7

      • 136
        21 Realm 0x000000010643f140 ZNKSt318functionIFvNS_10shared_ptrIN5realm5RealmEEES4_RNS2_6SchemaEEEclES4_S4_S6 + 100
        22 Realm 0x000000010643ef98 ZZN5realm5Realm13update_schemaENS_6SchemaEyNSt3_18functionIFvNS2_10shared_ptrIS0_EES5_RS1_EEENS3_IFvS5_EEEbENK3$_2clEv + 216
        23 Realm 0x000000010643eea0 ZNSt31L8invokeIRZN5realm5Realm13update_schemaENS1_6SchemaEyNS_8functionIFvNS_10shared_ptrIS2_EES6_RS3_EEENS4_IFvS6_EEEbE3$_2JEEEDTclclsr3std31E7forwardIT_Efp_Espclsr3std31E7forwardIT0_Efp0_EEEOSE_DpOSF + 28
        24 Realm 0x000000010643ee50 ZNSt3128invoke_void_return_wrapperIvE6callIJRZN5realm5Realm13update_schemaENS3_6SchemaEyNS_8functionIFvNS_10shared_ptrIS4_EES8_RS5_EEENS6_IFvS8_EEEbE3$_2EEEvDpOT + 28
        25 Realm 0x000000010643ee28 ZNSt3110function12_alloc_funcIZN5realm5Realm13update_schemaENS2_6SchemaEyNS_8functionIFvNS_10shared_ptrIS3_EES7_RS4_EEENS5_IFvS7_EEEbE3$_2NS_9allocatorISD_EEFvvEEclEv + 28
        26 Realm 0x000000010643d710 ZNSt3110function6_funcIZN5realm5Realm13update_schemaENS2_6SchemaEyNS_8functionIFvNS_10shared_ptrIS3_EES7_RS4_EEENS5_IFvS7_EEEbE3$_2NS_9allocatorISD_EEFvvEEclEv + 28
        27 Realm 0x00000001061ab5a4 ZNKSt3110function12_value_funcIFvvEEclEv + 60
        28 Realm 0x00000001061a0bcc ZNKSt3_18functionIFvvEEclEv + 24
        29 Realm 0x00000001061a076c ZN5realm11ObjectStore20apply_schema_changesERNS_11TransactionEyRNS_6SchemaEyNS_10SchemaModeERKNSt3_16vectorINS_12SchemaChangeENS6_9allocatorIS8_EEEENS_4util8OptionalINS6_12basic_stringIcNS6_11char_traitsIcEENS9_IcEEEEEENS6_8functionIFvvEEE + 668
        30 Realm 0x0000000106433564 ZN5realm5Realm13update_schemaENS_6SchemaEyNSt3_18functionIFvNS2_10shared_ptrIS0_EES5_RS1_EEENS3_IFvS5_EEEb + 1076
        31 Realm 0x000000010637c974 +[RLMRealm realmWithConfiguration:queue:error:] + 3052
        32 Realm 0x000000010637bd28 +[RLMRealm realmWithConfiguration:error:] + 84
        33 MyApp 0x0000000100da229c $sSo8RLMRealmC13configurationABSo0A13ConfigurationC_tKcfCTO + 148
        34 MyApp 0x0000000100da1a34 $s8 MyApp 7DBToolsC8getRealmSo8RLMRealmCSgyFZ + 3044
        35 MyApp 0x0000000100c1f19c $s8 MyApp 11UserRequestC10getProfile10completionyySSSg_s5Error_pSgtc_tFZy9Alamofire12DataResponseVyypGcfU_ + 2764
        36 Alamofire 0x000000010335ca44 $s9Alamofire11DataRequestC8response5queue0D10Serializer17completionHandlerACXDSo012OS_dispatch_E0CSg_xyAA0B8ResponseVy16SerializedObjectQzGctAA0bkF8ProtocolRzlFyycfU_yycfU_ + 368
        37 Alamofire 0x00000001033152bc $sIeg_IeyB_TR + 56
        38 libdispatch.dylib 0x0000000108a3e338 _dispatch_call_block_and_release + 24
        39 libdispatch.dylib 0x0000000108a3f730 _dispatch_client_callout + 16
        40 libdispatch.dylib 0x0000000108a4d710 _dispatch_main_queue_callback_4CF + 976
        41 CoreFoundation 0x00000001a66197fc 50CF3336-313F-3A7D-9048-CB1ED8EC3368 + 690172
        42 CoreFoundation 0x00000001a66146d0 50CF3336-313F-3A7D-9048-CB1ED8EC3368 + 669392
        43 CoreFoundation 0x00000001a6613ce8 CFRunLoopRunSpecific + 424
        44 GraphicsServices 0x00000001b075e38c GSEventRunModal + 160
        45 UIKitCore 0x00000001aa742444 UIApplicationMain + 1932
        46 MyApp 0x000000010105772c main + 80
        47 libdyld.dylib 0x00000001a649b8f0 3D6D64B4-CB2B-3CC4-A7E9-02774DF7AE74 + 6384!!! IMPORTANT: Please send this log and info about Realm SDK version and other relevant reproduction info to help@realm.io.
        </code>
        </pre>

      Code Sample

      <pre>
      <code>
      if (oldSchemaVersion < 14) {
      migration.renameProperty(forClass: "BDraft", oldName: "draft_id", newName: "draftId")
      migration.renameProperty(forClass: "BDraft", oldName: "bien", newName: "object")
      migration.renameProperty(forClass: "BDraft", oldName: "products_id", newName: "objectId")
      migration.enumerateObjects("Draft") { oldObject, newObject in
      if let new = newObject, let type = new["draftType"] as? Int, type == 0 {
      if let type = new["draftModel"] as? Int, type == 0

      { let draft = BDraft() draft.draftId = "\(new["draft_id"] as? String ?? "")_\(NSUUID().uuidString)" draft.draftType = .draft draft.lastUpdate = new["lastUpdate"] as? String draft.object = new["object"] as? Data draft.objectId = new["object_id"] as? Int ?? 0 migration.createObject("BDraft", withValue: draft) }

      else if let type = new["draftModel"] as? Int, type == 1

      { let draft = CDraft() draft.draftId = "\(new["draft_pid"] as? String ?? "")_\(NSUUID().uuidString)" draft.draftType = .draft draft.lastUpdate = new["lastUpdate"] as? String draft.object = new["object"] as? Data draft.objectId = new["object_id"] as? Int ?? 0 migration.createObject("CDraft", withValue: draft) }

      }
      }
      }
      </code>
      </pre>

      <pre>
      <code>
      @objc
      enum DraftType: Int {
      case draft
      case pending
      case sent
      }

      @objc
      enum DraftModel: Int {
      case b
      case c
      }

      @objcMembers
      public class Draft: RLMObject {
      dynamic var draft_id: String?
      dynamic var draftType: DraftType = .draft
      dynamic var draftModel: DraftModel = .b
      dynamic var lastUpdate: String?
      dynamic var object: Data?
      dynamic var object_id: Int = 0

      override public class func primaryKey() -> String? {
          return "draft_id"
      }
      
      required convenience init!(_ objectId: String) {
          self.init()
          
          self.draft_pid = objectId
      }
      

      }
      </code>
      </pre>

      <pre>
      <code>
      @objcMembers
      class BDraft: RLMObject {
      dynamic var draftId: String?
      dynamic var draftType: DraftType = .draft
      dynamic var lastUpdate: String?
      dynamic var object: Data?
      dynamic var objectId: Int = 0

      override public class func primaryKey() -> String? {
          return "draftId"
      }
      
      required convenience init!(_ objectId: String) {
          self.init()
          
          self.draftId = objectId
      }
      

      }
      </code>
      </pre>

      <pre>
      <code>
      @objcMembers
      class CDraft: RLMObject {
      dynamic var draftId: String?
      dynamic var draftType: DraftType = .draft
      dynamic var lastUpdate: String?
      dynamic var object: Data?
      dynamic var objectId: Int = 0

      override public class func primaryKey() -> String? {
          return "draftId"
      }
      
      required convenience init!(_ objectId: String) {
          self.init()
          
          self.draftId = objectId
      }
      

      }
      </code>
      </pre>

      Version of Realm and Tooling

      Realm framework version: 5.1.0
      Xcode version: 11.5
      iOS version: 13.5.1

            Assignee:
            thomas.goyne@mongodb.com Thomas Goyne
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: