Search code examples
javacouchdbcloudant

Cannot put DesignDocuments back


I am trying to delete all documents from a database but i want to preserve the views. So i tried

//First, get all DesignDocument for the current database
List<DesignDocument> dbDesigns = cloudant.getDesignDocumentManager().list();

//Now, we delete the database           
cloudantClient.deleteDB(_DatabaseName);

            //now we create the database again
cloudant = cloudantClient.database(_DatabaseName, true);

//finally, try to add the DesignDocuments back
            if (dbDesigns != null && dbDesigns.size() > 0) {
                for (DesignDocument dDoc : dbDesigns) {
                    Response response = cloudant.getDesignDocumentManager().put(dDoc);
                    System.out.println(response);
                }
            }

but i get error at

Response response = cloudant.getDesignDocumentManager().put(dDoc);


java.lang.IllegalArgumentException: rev should be null

    at com.cloudant.client.org.lightcouch.internal.CouchDbUtil.assertNull(CouchDbUtil.java:72)
    at com.cloudant.client.org.lightcouch.CouchDbClient.put(CouchDbClient.java:410)
    at com.cloudant.client.org.lightcouch.CouchDbClient.put(CouchDbClient.java:394)
    at com.cloudant.client.org.lightcouch.CouchDatabaseBase.save(CouchDatabaseBase.java:196)
    at com.cloudant.client.api.Database.save(Database.java:710)
    at com.cloudant.client.api.DesignDocumentManager.put(DesignDocumentManager.java:122)

is there any other way to preserve the views?


Solution

  • I'm suspecting the error is raised because the document revision property (_rev) is set in dDoc. However, since a document with a matching id is not found in the database the put method raises an error. Try setting the revision to null using the setRevision method prior to invoking put

    dDoc.setRevision(null);
    Response response = cloudant.getDesignDocumentManager().put(dDoc);