2013-07-28 22 views
5

Tôi có cơ sở dữ liệu của Tổ chức trong MongoDB. Tôi đang cố gắng lưu dữ liệu trong cơ sở dữ liệu đó bằng cách sử dụng mongoengine. Tôi đang sử dụng máy chủ Djnago. khi tôi đang tạo ra đối tượng thì nó hoạt động tốt nhưng sau khi chỉnh sửa nó đưa ra một số lỗi.OperationError: Không thể lưu tài liệu (LEFT_SUBFIELD chỉ hỗ trợ Object: tổ tiên.0 không: 7)

class Organization(Document): 
    username= StringField() 
    ancestors = ListField(ReferenceField('Organization',dbref=False), default = list) 
    parents = ListField(ReferenceField('Organization',dbref=False),default = list) 
    descendants = ListField(ReferenceField('Organization',dbref=False), default = list) 


obj1 = Organization(username = 'kousik') 
obj1.save() 
<Organization: Organization object> #obj1 created 

obj2 = Organization(username = 'chowdhury',ancestors = [obj1],parents=[obj1]) 
obj2.save() 
<Organization: Organization object> #obj2 created 

obj3 = Organization(username = 'kchowdhury',ancestors = [obj1,obj2],parents=[obj2]) 
obj3.save() 
<Organization: Organization object> #obj3 creaed 

obj1.descendants = [obj2,obj3] 
obj1.save() 
<Organization: Organization object> #obj1 updated 

obj2.descendants = [obj3] 
obj2.save() 

Traceback (most recent call last): 
    File "<pyshell#43>", line 1, in <module> 
    obj2.save() 
    File "C:\Python27\lib\site-packages\mongoengine\document.py", line 267, in save 
    raise OperationError(message % unicode(err)) 
OperationError: Could not save document (LEFT_SUBFIELD only supports Object: ancestors.0 not: 7) 

Trả lời

4

Đây là lỗi mongoengine. Tôi đã tạo sự cố cho điều này: https://github.com/MongoEngine/mongoengine/issues/422.

Giải pháp cho bây giờ:

  1. Sử dụng đầy đủ tài liệu tải lại trước khi cập nhật (chỉ reload không làm việc, bởi vì nghỉ referencies):

    obj1 = Organization.objects(username=obj1.username).first() 
    obj1.descendants = [obj2, obj3] 
    obj1.save() 
    
    
    obj2 = Organization.objects(username=obj2.username).first() 
    obj2.descendants = [obj3] 
    obj2.save() 
    
  2. Sử dụng cập nhật nguyên tử thay vì save.

  3. Sử dụng to_dbref cho referencies:

    obj1 = Organization(username='kousik') 
    print obj1.save() 
    # <Organization: Organization object> #obj1 created 
    
    obj2 = Organization(username='chowdhury', ancestors=[obj1.to_dbref()], parents=[obj1.to_dbref()]) 
    print obj2.save() 
    # <Organization: Organization object> #obj2 created 
    
    obj3 = Organization(username='kchowdhury', ancestors=[obj1.to_dbref(), obj2.to_dbref()], parents=[obj2.to_dbref()]) 
    print obj3.save() 
    # <Organization: Organization object> #obj3 creaed 
    
    obj1.descendants = [obj2.to_dbref(), obj3.to_dbref()] 
    print obj1.save() 
    # <Organization: Organization object> #obj1 updated 
    
    obj2.descendants = [obj3.to_dbref()] 
    print obj2.save() 
    # <Organization: Organization object> #obj2 updated 
    
+0

Thank you very much – Nullify

+0

sẽ được cố định trong 0.8.4 nhờ @tbicr cho vé – Ross