3

I was reading 'CouchDB: The Definitive Guide' and I was confused by this paragraph:

For demoing purposes, having CouchDB assign a UUID is fine. When you write your first programs, we recommend assigning your own UUIDs. If your rely on the server to generate the UUID and you end up making two POST requests because the first POST request bombed out, you might generate two docs and never find out about the first one because only the second one will be reported back. Generating your own UUIDs makes sure that you’ll never end up with duplicate documents.

I thought that uuids (specifically the _id) were saved only when the document creation was successful. That is, when I "post" an insert request for a new document, the _id is generated automatically. If the document is saved then the field is kept, otherwise discarded. Is that not the case?

Can you please explain what is the correct way to generate _id fields in CouchDB?

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Akshat Jiwan Sharma
  • 15,430
  • 13
  • 50
  • 60

2 Answers2

5

I think this quote is not really about UUIDs but about using PUT (which is idempotent) instead of POST.

Check this thread for more information : Consequences of POST not being idempotent (RESTful API)

Community
  • 1
  • 1
Aurélien Bénel
  • 3,775
  • 24
  • 45
  • Well, you're not obliged to. But if you experience duplicates because of timeouts, this is the way to go. However this requires a bit more work, since you must generate the UUID yourself. – Aurélien Bénel Jan 05 '13 at 11:49
  • 3
    One last thing: you can also ask CouchDB to generate UUIDs for later use. http://wiki.apache.org/couchdb/HttpGetUuids – Aurélien Bénel Jan 05 '13 at 11:58
2

I think that quote is wrong or out of date, and it's fine to rely on CouchDB for ID generation. I've used this at work a lot and have never really run into any issues.

djc
  • 11,603
  • 5
  • 41
  • 54