当我将mongoengine从0.8.6更新到0.9.0时,无法获取旧文档

时间:2015-09-09 10:17:34

标签: mongodb flask mongoengine

我使用mongodb和mongoengine 0.8.6和烧瓶用于网络框架 当我将我的mongoengine更新为0.9.0时,我无法在数据库中获取旧文档但是当我创建一个新的Document时,我没有问题可以获得它! 提出的错误是:

raise FieldDoesNotExist(msg)
FieldDoesNotExist: The field 'notification_actions' does not exist on the document 'User'

当我删除上面提到的字段时(例如在这种情况下notification_actions),它引发了另一个field的错误,让我感到惊讶的是新的Document我创建具有相同的field s!

班级定义:

class User(Document):
    stores = EmbeddedDocumentListField(UserStore)
    password = StringField(max_length=255, required=True)
    email = StringField(max_length=255, required=True)
    forget_password_key = StringField()
    mail_verify_key = StringField()
    firstName = StringField(max_length=45)
    lastName = StringField(max_length=45)
    marital = StringField(max_length=1)
    address = StringField()
    state = StringField()
    city = StringField()
    postal = IntField()
    websites = ListField(StringField(max_length=65))
    sex = StringField(max_length=1)
    interest = StringField()
    birthday = DateTimeField()
    skills = StringField()
    phone = StringField()
    cellphone = StringField()
    fax = StringField()
    avatar = StringField()
    msg = IntField(default=0)
    group = StringField()
    created_at = DateTimeField(default=datetime.datetime.now, required=True)

我试图获得它的Document但是mongoengine会引发错误:

{
    "_id" : ObjectId("55c74ba4e3615342fd0b541c"),
    "_keywords" : [],
    "address" : "",
    "banks" : [ 
        {
            "id" : "55518df6123dbb06402f0e0b",
            "bank_name" : "mellat",
            "account_num" : "603799112344011",
            "card_num" : "223333"
        }
    ],
    "categories" : [],
    "cellphone" : "",
    "city" : "13",
    "created_at" : ISODate("2015-04-01T01:03:18.140Z"),
    "deliveries" : [],
    "delivery" : {
        "courier" : true,
        "post_custom_made" : true,
        "post_leading" : true,
        "free_for_price" : 0,
        "area_support" : "",
        "price_for_area_selected" : 0
    },
    "description" : "",
    "domain" : "ehsanco",
    "domains" : [ 
        {
            "domain" : "ehsanco.bab.ir",
            "is_active" : true,
            "can_delete" : false,
            "is_default" : true
        }, 
        {
            "domain" : "ielecom.org",
            "is_active" : false,
            "can_delete" : true,
            "is_default" : false
        }
    ],
    "email" : "emahmoudi98@gmail.com",
    "external_domains" : [ 
        {
            "domain" : "ielecom.org",
            "is_active" : false,
            "must_buy" : false
        }
    ],
    "fax" : "",
    "inventories" : [ 
        {
            "store" : ObjectId("551b048e123dbb0524c04983")
        }
    ],
    "is_dealer" : false,
    "logo" : "download",
    "marketplace" : "bab.ir",
    "menu" : [ 
        ObjectId("55b60037123dbb4e200560f6")
    ],
    "msg" : 0.0000000000000000,
    "notification_actions" : [ 
        {
            "name" : "buy",
            "has_email" : true,
            "has_sms" : false,
            "has_site_alert" : true
        }, 
        {
            "name" : "comment",
            "has_email" : true,
            "has_sms" : false,
            "has_site_alert" : true
        }, 
        {
            "name" : "pay",
            "has_email" : true,
            "has_sms" : false,
            "has_site_alert" : true
        }, 
        {
            "name" : "contact",
            "has_email" : true,
            "has_sms" : false,
            "has_site_alert" : true
        }, 
        {
            "name" : "out_of_stock",
            "has_email" : true,
            "has_sms" : false,
            "has_site_alert" : true
        }, 
        {
            "name" : "new_user",
            "has_email" : true,
            "has_sms" : false,
            "has_site_alert" : true
        }
    ],
    "password" : "pbkdf2:sha1:1000$1bca0pNG$b3d7085bf5090057af6fa79e617a5830ac72793e",
    "pay_way" : [],
    "payment" : {
        "zarinpal_is_active" : true,
        "cart_to_card_is_active" : true,
        "coordinatad_is_active" : true
    },
    "phone" : "",
    "related_store" : [],
    "state" : "8",
    "store" : ObjectId("551b048e123dbb0524c04983"),
    "store_name" : "ehsanco",
    "stores" : null,
    "theme" : ObjectId("55850cb7123dbb7c6e1590a6"),
    "theme_history" : [ 
        ObjectId("55094c6a123dbb2e1c280c0b"), 
        ObjectId("55094c6a123dbb2e1c280c0c"), 
        ObjectId("55850cb7123dbb7c6e1590a6")
    ],
    "websites" : [],
    "zarinpal" : {
        "zarinpal_id" : "zp.118902",
        "valet_id" : 1
    }
}

以及我可以从mongoengine获取的Document

{
    "_id" : ObjectId("55c74ba4e3615342fd0b5425"),
    "banks" : [],
    "categories" : [],
    "created_at" : ISODate("2015-07-01T08:55:43.052Z"),
    "devices" : [],
    "domains" : [ 
        {
            "domain" : "khallaghi.bab.ir",
            "is_active" : true,
            "can_delete" : false,
            "is_default" : true
        }
    ],
    "email" : "mdkhallaghi@gmail.com",
    "freeze" : false,
    "inventories" : [ 
        {
            "store" : ObjectId("55936bc7123dbb512c534206")
        }
    ],
    "is_dealer" : false,
    "keywords" : [],
    "marketplace" : "bab.ir",
    "menu" : [],
    "msg" : 0.0000000000000000,
    "notification_actions" : [ 
        {
            "name" : "comment",
            "has_email" : true,
            "has_sms" : false,
            "has_site_alert" : true
        }, 
        {
            "name" : "buy",
            "has_email" : true,
            "has_sms" : false,
            "has_site_alert" : true
        }, 
        {
            "name" : "new_user",
            "has_email" : true,
            "has_sms" : false,
            "has_site_alert" : true
        }, 
        {
            "name" : "pay",
            "has_email" : true,
            "has_sms" : false,
            "has_site_alert" : true
        }, 
        {
            "name" : "contact",
            "has_email" : true,
            "has_sms" : false,
            "has_site_alert" : true
        }, 
        {
            "name" : "out_of_stack",
            "has_email" : true,
            "has_sms" : false,
            "has_site_alert" : true
        }
    ],
    "ordering_freeze" : false,
    "password" : "pbkdf2:sha1:1000$99sS9KCg$572b4c418fe0f7cd6b2d0e303f622353aae0a6a0",
    "pay_way" : [],
    "redirect_all_domain_to_default" : true,
    "related_store" : [],
    "store" : ObjectId("55936bc7123dbb512c534206"),
    "store_name" : "khallaghi",
    "stores" : null,
    "tax" : 0.0000000000000000,
    "theme" : ObjectId("55850cb7123dbb7c6e15909e"),
    "theme_history" : [],
    "websites" : []
}

我写的查询:

tmp = User.objects(email=email).count()
users = User.objects(
    email=email
    ).first()

有趣的是第一个查询正常运行,但第二个查询引发错误

完整错误:

    Traceback (most recent call last):
  File "/usr/lib/python2.7/logging/__init__.py", line 859, in emit
    msg = self.format(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 732, in format
    return fmt.format(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 471, in format
    record.message = record.getMessage()
  File "/usr/lib/python2.7/logging/__init__.py", line 331, in getMessage
    msg = str(self.msg)
  File "/home/mohammad/shopify/env/local/lib/python2.7/site-packages/mongoengine/queryset/queryset.py", line 58, in __repr__
    self._populate_cache()
  File "/home/mohammad/shopify/env/local/lib/python2.7/site-packages/mongoengine/queryset/queryset.py", line 93, in _populate_cache
    self._result_cache.append(self.next())
  File "/home/mohammad/shopify/env/local/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 1359, in next
    _auto_dereference=self._auto_dereference, only_fields=self.only_fields)
  File "/home/mohammad/shopify/env/local/lib/python2.7/site-packages/mongoengine/base/document.py", line 709, in _from_son
    obj = cls(__auto_convert=False, _created=created, __only_fields=only_fields, **data)
  File "/home/mohammad/shopify/env/local/lib/python2.7/site-packages/mongoengine/base/document.py", line 81, in __init__
    raise FieldDoesNotExist(msg)
FieldDoesNotExist: The field 'domain' does not exist on the document 'User'
Logged from file extended_logging.py, line 113
127.0.0.1 - - [09/Sep/2015 15:24:53] "POST /store/login/ HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/mohammad/shopify/env/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/mohammad/shopify/env/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/mohammad/shopify/env/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/mohammad/shopify/env/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/mohammad/shopify/env/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/mohammad/shopify/env/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/mohammad/shopify/env/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/mohammad/shopify/env/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/mohammad/shopify/project/utils/auth.py", line 53, in decorated_function
    return f(*args, **kwargs)
  File "/home/mohammad/shopify/project/utils/template.py", line 16, in decorated_function
    return f(*args, **kwargs)
  File "/home/mohammad/shopify/project/apps/store/views/auth.py", line 52, in login
    user = users[0]
  File "/home/mohammad/shopify/env/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 161, in __getitem__
    _auto_dereference=self._auto_dereference, only_fields=self.only_fields)
  File "/home/mohammad/shopify/env/lib/python2.7/site-packages/mongoengine/base/document.py", line 709, in _from_son
    obj = cls(__auto_convert=False, _created=created, __only_fields=only_fields, **data)
  File "/home/mohammad/shopify/env/lib/python2.7/site-packages/mongoengine/base/document.py", line 81, in __init__
    raise FieldDoesNotExist(msg)
FieldDoesNotExist: The field 'domain' does not exist on the document 'User'

错误的总结是当我调用first()函数时,错误会引发错误并且表示无法在domain集合中找到User字段

0 个答案:

没有答案