lanto03 / couchdb-python

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

Can't specify '_id' when trying to save a document #187

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I've got a bunch of json dicts with a primary key specified for the key 'pk'. I 
figured I might as well let couch use the same pk. So I tried the following:

    raw_json_data = urllib.urlopen(gluon_url).read()
    json_data = json.loads(raw_json_data)
    couch = couchdb.Server(couch_url)
    db = couch.create('partsd')
    for i, part in enumerate(json_data):
        part['_id'] = part['pk']
        db.save(part)

I get the following error:

Traceback (most recent call last):
  File "sync.py", line 40, in <module>
    main(sys.argv)
  File "sync.py", line 36, in main
    sync_data(opt.db_url, opt.couch_url)
  File "sync.py", line 24, in sync_data
    db.save(part)
  File "/tmp/couch/lib/python2.6/site-packages/CouchDB-0.8-py2.6.egg/couchdb/client.py", line 404, in save
    func = self.resource(doc['_id']).put_json
  File "/tmp/couch/lib/python2.6/site-packages/CouchDB-0.8-py2.6.egg/couchdb/http.py", line 365, in __call__
    obj = type(self)(urljoin(self.url, *path), self.session)
  File "/tmp/couch/lib/python2.6/site-packages/CouchDB-0.8-py2.6.egg/couchdb/http.py", line 501, in urljoin
    path = '/'.join([''] + [quote(s) for s in path])
  File "/tmp/couch/lib/python2.6/site-packages/CouchDB-0.8-py2.6.egg/couchdb/http.py", line 453, in quote
    return urllib.quote(string, safe)
  File "/usr/lib/python2.6/urllib.py", line 1224, in quote
    res = map(safe_map.__getitem__, s)
TypeError: argument 2 to map() must support iteration

If I specify the 'id' instead of '_id', it works. The documentation for .save 
seems to indicate that I should be able to set _id.

Original issue reported on code.google.com by matthewh...@gmail.com on 27 Jun 2011 at 10:02

GoogleCodeExporter commented 8 years ago
A doc's _id must be a string. My guess is that part['pk'] is an integer.

Original comment by matt.goo...@gmail.com on 27 Jun 2011 at 10:17

GoogleCodeExporter commented 8 years ago
No further information, presumably invalid.

Original comment by djc.ochtman on 21 Sep 2012 at 8:09