Search code examples
mongodbpython-2.7flaskmongoengine

How to fix mongoengine dereference error after the migration to 0.8?


I've just upgraded mongoengine and now I'm getting an error. Currently, models have been defined as following:

class Descriptor(Document):
    root = ReferenceField(DescriptorNode, required=True)
    (..)

class DescriptorNode(Document):
    channel = ReferenceField(Channel, required=True)
    (..)

class Channel(Document):
    (..)

Using DBRef = True, this is the current code I used with mongoengine==0.7.10:

descriptor =  Descriptor.objects.get(id = xxxx)
channel = descriptor.root.channel

I've chosen to remove DBRef = True so as to migrate to mongoengine==0.8.1 and then rebuild the database.

Now I'm getting this error:

channel = descriptor.root.channel
AttributeError: channel
/site-packages/bson/dbref.py Line:88

I tried select_related(), but nothing seems to work properly.

Descriptor:

{
    "_id" : ObjectId("51ae3f6ba2aa1c0a32998952"),
    "created_date" : ISODate("2013-06-04T19:26:35.630Z"),
    "modified_date" : ISODate("2013-06-05T12:45:55.570Z"),
    "sequence" : 843174,
    "groups" : [ ],
    "name" : "desktop-BR",
    "root" : ObjectId("51ae3f6ba2aa1c0a329988b0"),
    "tree_hash" : "97e3716db74543e66a11405e9e04185452183ac1"
}

DescriptorNode:

{
    "_id" : ObjectId("51ae3f6aa2aa1c0a3299885a"),
    "channel" : ObjectId("51ae0f06a2aa1c0a327f3958"),
    "created_date" : ISODate("2013-06-04T19:26:34.235Z"),
    "groups" : [ ],
    "is_prioritized" : false,
    "is_published" : true,
    "modified_date" : ISODate("2013-06-05T12:46:02.704Z"),
    "order" : 0,
    "sequence" : 842926
}

Solution

  • Did you do as similar as example?

    Firstly, to change DBRef = True to DBRef = False. Secondly, to migrate by hands:

    for d in Descriptor.objects():
        d.root = d.root
        d.save()
    
    for d in DescriptorNode.objects():
        d.channel = d.channel
        d.save()