couchdb-python指定自己的_id失败



如何在couchdb python(0.9)中定义自己的_id,因为当我尝试'_id': i[5]时,我得到了以下错误消息?

$ python test3.py
828288
Traceback (most recent call last):
  File "test3.py", line 42, in <module>
    db.save(doc)
  File "/home/mictadlo/.virtualenvs/unisnp/lib/python2.7/site-packages/couchdb/client.py", line 415, in save
    func = _doc_resource(self.resource, doc['_id']).put_json
  File "/home/mictadlo/.virtualenvs/unisnp/lib/python2.7/site-packages/couchdb/client.py", line 954, in _doc_resource
    if doc_id[:1] == '_':
TypeError: 'int' object has no attribute '__getitem__'

以下是导致上述错误的脚本:

from couchdb.mapping import Document, TextField, IntegerField, Mapping
from couchdb.mapping import DictField, ViewField, BooleanField, ListField
from couchdb import Server
# $ sudo systemctl start couchdb
# http://localhost:5984/_utils/
server = Server()
db = server.create("test")
r = [["Test", "A", "B01", 828288,  1,    7, 'C', 5],
    ["Test", "A", "B01", 828288,  1,    7, 'T', 6],
    ["Test", "A", "B01", 171878,  3,    8, 'C', 5],
    ["Test", "A", "B01", 171878,  3,    8, 'T', 6],
    ["Test", "A", "B01", 871963,  3,    9, 'A', 5],
    ["Test", "A", "B01", 871963,  3,    9, 'G', 6],
    ["Test", "A", "B01", 1932523, 1,   10, 'T', 4],
    ["Test", "A", "B01", 1932523, 1,   10, 'A', 5],
    ["Test", "A", "B01", 1932523, 1,   10, 'X', 6],
    ["Test", "A", "B01", 667214,  1,   14, 'T', 4],
    ["Test", "A", "B01", 667214,  1,   14, 'G', 5],
    ["Test", "A", "B01", 667214,  1,   14, 'G', 6]]

for i in r:
    print i[3]
    doc = {
        'type': i[0],
        'name': i[1],
        'sub_name': i[2],
        'pos': i[3],
        's_type': i[4],
        '_id': i[5],
        'chr':[]
    }
    doc['chr'].append({
        "letter":i[6],
        "no":i[7]
    })
    db.save(doc)

它期望_id是一个字符串,并且您正在传递一个int类型。错误是由以下行引起的:

if doc_id[:1] == '_':

因为脚本试图对int对象进行切片。

所以将其更改为字符串类型:

...
...
'_id': str(i[5]),
...

相关内容

最新更新