3

Based on the RethinkDB replace() docs I am trying to change the primary key of a document. In this case, the primary key is email:

var renamePerson = function(originalEmail, newEmail, cb){
    rethink.db(DATABASE).table(TABLE).get(originalEmail).replace({email: newEmail}).run(dbConnection, cb)
}

This should work, but unchanged is 1, and looking up the new document doesn't seem to return any results.

How can I change the primary key of a document?

mikemaccana
  • 110,530
  • 99
  • 389
  • 494

1 Answers1

0

Doing some research, I found this quote from the developers:

'We don't let people change pkeys, they have to delete and reinsert the document instead.'

Based on that:

var renamePerson = function(originalEmail, newEmail, cb){
    runningDatabase.getPersonByEmail(originalEmail, function(err, person){
        if ( err ) {
            log('Error finding person to rename', err)
            cb(err)
            return
        }
        person.email = newEmail;
        createPersonRaw(person, function(err, createdPerson){
            if ( err ) {
                log('>>> Err creating new document for renamed person', err)
                cb(err)
                return
            }
            deletePerson(originalEmail, cb)
        })
    })
}

(createPersonRaw is just a helper function that inserts a doc, deletePerson marks a person as being deleted)

mikemaccana
  • 110,530
  • 99
  • 389
  • 494