Search code examples
phpcouchdbcloudant

Cloudant CouchDB Document Update Conflict


For some reason, I'm getting the following error:

Fatal error: Uncaught exception 'PHPOnCouch\Exceptions\CouchConflictException' with message 'Conflict - Document update conflict.

Fatal error: Uncaught exception 'PHPOnCouch\Exceptions\CouchConflictException' with message 'Conflict - Document update conflict. (PUT /u12345/678bb21541f0ed5e67df1a9070000b55 [])' in /.../vendor/popojargo/php-on-couch/src/Exceptions/CouchException.php:74 
Stack trace: 
    #0 /.../vendor/popojargo/php-on-couch/src/CouchClient.php(168): PHPOnCouch\Exceptions\CouchException::factory(Array, 'PUT', '/u12345...', Array) 
    #1 /.../vendor/popojargo/php-on-couch/src/CouchClient.php(582): PHPOnCouch\CouchClient->_queryAndTest('PUT', '/u12345...', Array, Array, Object(stdClass)) 
    #2 /.../test.php(46): PHPOnCouch\CouchClient->storeDoc(Object(stdClass)) 
    #3 /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Res in /.../vendor/popojargo/php-on-couch/src/Exceptions/CouchException.php on line 74

I remove the revision so I'm not sure why else a document update conflict would occur.

Does anyone have any insight into this? It would be greatly appreciated.

The Objects look like this

{
  "_id": "678bb21541f0ed5e67df1a9070000b55",
  "_rev": "3-c0ea1980545a80839677d658eec0df78",
  "name": "Other",
  "timestamp": 0
}

My Code

$userClient = new CouchClient("http://admin:pass@localhost:8080", "u12345");

$all_documents = $userClient->include_docs(TRUE)->getAllDocs();

foreach ($all_documents->rows as $d) {
    $doc = $d->doc;

    unset($doc->_rev);
    $doc->timestamp = 0;
    $userClient->storeDoc($doc);
}

Solution

  • When you update a document, you need to provide the _id. If this document already exists, you have to supply the _rev.

    As you remove the _rev from the local copy of your document, updating it results of a conflict. Why? You provided a document without _rev and they expect you to provide the latest _rev to acknowledge you that this will be the next version.