lanto03 / couchdb-python

Automatically exported from code.google.com/p/couchdb-python
Other
0 stars 0 forks source link

Setting the _id field is not passed on to the DB #39

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hello,
   Sorry if I'm missing something obvious, I only started playing with CouchDB yesterday, and it's been a long 
week... ;)

If I create a document object:

>>> dataset = client.Document()
>>> dataset['_id'] =  "/CSA07AllEvents/CMSSW_1_6_9-FastSim-1204301968/AOD"
>>> dataset['key'] = ["/CSA07AllEvents/CMSSW_1_6_9-FastSim-1204301968/AOD", 0]
>>> db.create(dataset)

I would expect that the _id of the object, once created in the db, would be the 
same as what I set it to. At the 
moment it is lost, and a new one auto generated. 

Likewise, if I do something like:

>>> doc = {
...     "_id": "the id I generate",
...     "key": ["the id I generate", 0]
... }
>>> db['thing'] = doc

the _id in the dictionary is over ridden by the db['thing'] (though I can 
understand that). 

I guess this would mean changing the Database.create() method, and given that's 
a short but sweet method 
maybe this isn't practical. Doing the db['id I want'] is fine, but I think it 
would be neater if this was wrapped up in 
the Document object (maybe it is and I'm missing something obvious...).

I ask because the application I'm evaluating using Couchdb (and it's python 
interface) for will have a lot of 
GUID's already in place, and I don't really want/need a new one.
Cheers
Simon

Original issue reported on code.google.com by simonmetson@googlemail.com on 3 Dec 2008 at 6:26

GoogleCodeExporter commented 8 years ago
Just read Document.store(), sorry for the noise!

Original comment by simonmetson@googlemail.com on 3 Dec 2008 at 11:42

GoogleCodeExporter commented 8 years ago
Database.create() should only be used when you want CouchDB to generate an ID 
for you. If you already have an 
ID, you should use:

  db[id] = doc

And yes, in that case, if the doc already has an _id field, it will be 
overwritten.

Maybe this is less clear than I had hoped, but I'm not sure making create() use 
an already supplied ID would be a 
clean solution.

Original comment by cmlenz on 1 Jul 2009 at 3:18