1

Ok, so I have a Realm database (named AutoComplete) with the following structure

enter image description here

I Now I do a call to the backend, to get make all the [Folder] items.

        Alamofire.request(.GET, URL).responseObject { (response: Response<Folders, NSError>) in
            if response.result.isSuccess {
                let mappedObject = response.result.value
                let realm = try! Realm()
                // open AutoComplete model
                let openAutoComplete = realm.objects(AutoComplete)
                try! realm.write {
                    if let everythingUnderResult = mappedObject?.result {
                        // for folder in [Folder]
                        for item in everythingUnderResult {
                       //check if the search string you typed in exists in the database                            
                        let searchifExists = openAutoComplete.filter("search == %@", searchString)
                            let makeString = searchifExists.first
                            let getSearchColumn = makeString?.search
                            if searchString == getSearchColumn {
                                //item exists, now add the folder to the autocomplete DB
                                makeString?.folder.append(item)
                                realm.add(makeString!,update: true)
                            } else {
                                print(item)
                                realm.add(item)
                                makeString?.folder.append(item)
                            }
                        }
                    }
                }
            } else {
                print("couldn't get mobileapp data")
            }
        }

    }

My issue is that I can't add the folderobjects to the database. The makeString?.folder.append(item) line of code gives back the following error:

Can't set primary key property 'id' to existing value '65502'.'

I know that the id already exists in my Folders model of the database, with the same ID, but I just want to reference it in the Autocomplete model. (not update it or overwrite it).

Anyone has an idea on how to fix this?

SoundShock
  • 485
  • 1
  • 3
  • 24
  • Would you be willing to share your model definitions, or at least the parts relevant to your code snippet? – AustinZ Jun 20 '16 at 18:24

1 Answers1

2

In this part of code

   if searchString == getSearchColumn {
                            //item exists, now add the folder to the autocomplete DB
                            makeString?.folder.append(item) // it's ok
                            realm.add(makeString!,update: true) // you don't need this
                        } else {
                            print(item)
                            realm.add(item)
                            makeString?.folder.append(item)
                        }

just remove

realm.add(makeString!,update: true)

You don't need to add this record one more time to the table.Adding it to the folder list is enough

  • I have a similar issue, http://stackoverflow.com/questions/40592350/realm-cant-create-object-with-existing-primary-key-value. I am not able to undestand the issue. please help – Deekshith Bellare Nov 14 '16 at 15:35