mongoengine 中的迁移:InvalidId [英] Migrations in mongoengine: InvalidId

查看:64
本文介绍了mongoengine 中的迁移:InvalidId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 mongoengine 并尝试进行简单的迁移.我有一个字段,我想将其从 StringField 迁移到 ReferenceField 到另一个对象.我计划手动进行迁移,首先根据来自旧 StringField 的字符串构造新对象,然后显式设置它.

I am working with mongoengine and trying to do a simple migration. I have a field which I would like to migrate from being a StringField to a ReferenceField to another Object. I planned on doing the migration by hand, first constructing the new object based on the string that comes from the old StringField and then setting it explicitly.

问题是,一旦我更改了字段类型,我什至无法再访问其中一个顶级文档.是否需要在我的文档的类代码中创建一个虚拟"字段作为新字段的占位符?这对我来说似乎很笨拙,所以我认为有更好的方法吗?

The problem is I cannot even access one of the top level documents anymore once I changed the field type. Is it required to create a "dummy" field in the my document's class code as a placeholder for the new field? This seems kludgy to me so I assume there is a better way?

这里是错误,这是因为从 DB (StringField) 出来的字段与引用字段不一致.

Here is the error, which is because the field coming out of the DB (StringField) is not consistent with a reference field.

/usr/lib/python2.7/site-packages/mongoengine/queryset/base.pyc in __getitem__(self, key)
    149                 return queryset._get_as_pymongo(queryset._cursor.next())
    150             return queryset._document._from_son(queryset._cursor[key],
--> 151                                                 _auto_dereference=self._auto_dereference)
    152         raise AttributeError
    153 

/usr/lib/python2.7/site-packages/mongoengine/base/document.pyc in _from_son(cls, son, _auto_dereference)
    568                 try:
    569                     data[field_name] = (value if value is None
--> 570                                         else field.to_python(value))
    571                     if field_name != field.db_field:
    572                         del data[field.db_field]

/usr/lib/python2.7/site-packages/mongoengine/fields.pyc in to_python(self, value)
    935            not isinstance(value, (DBRef, Document, EmbeddedDocument))):
    936             collection = self.document_type._get_collection_name()
--> 937             value = DBRef(collection, self.document_type.id.to_python(value))
    938         return value
    939 

/usr/lib/python2.7/site-packages/mongoengine/base/fields.pyc in to_python(self, value)
    390     def to_python(self, value):
    391         if not isinstance(value, ObjectId):
--> 392             value = ObjectId(value)
    393         return value
    394 

/usr/lib/python2.7/site-packages/bson/objectid.pyc in __init__(self, oid)
     88             self.__generate()
     89         else:
---> 90             self.__validate(oid)
     91 
     92     @classmethod

/usr/lib/python2.7/site-packages/bson/objectid.pyc in __validate(self, oid)
    192                     raise InvalidId("%s is not a valid ObjectId" % oid)
    193             else:
--> 194                 raise InvalidId("%s is not a valid ObjectId" % oid)
    195         else:
    196             raise TypeError("id must be an instance of (%s, %s, ObjectId), "

InvalidId: Was Dirty: a2111fe89383bb562738b81c2b63fe78e877ed32 is not a valid ObjectId

推荐答案

我为您的迁移脚本建议的 2 个选项:

The 2 options I would suggest for your migration script:

  • 在必须迁移的字段上使用 DynamicField 应该允许您读取 ObjectIds 并存储回 DBRefs
  • 直接在 pymongo 中进行迁移(pymongo 的集合可通过 Person._get_collection() 访问)并循环遍历项目,更新 &保存

这篇关于mongoengine 中的迁移:InvalidId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆