Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoengine ListField(ReferenceField()) and custom primary_key

I have some pretty simple code:

from mongoengine import *

class Comment(Document):
    id = IntField(primary_key=True)
    text = StringField()

class Message(Document):
    id = IntField(primary_key=True)
    comments = ListField(ReferenceField(Comment))

connect('test_db')

c1 = Comment(id=1)
c1.text = 'message_one'
c1.save()

c2 = Comment(id=2)
c2.text = 'message_two'
c2.save()

m = Message(id=1)
m.comments = [c1, c2]
m.save()

msg = Message.objects.get(id=1)
for comment in msg.comments:
    print comment.id, comment.text

I've expected that it will print

1 message_one

2 message_two

but I've got

1 message_one

1 message_one

When I use any mongodb admin UI to view database everything seems to be ok:

{ "_cls" : "Message" , "_id" : 1 , "_types" : [ "Message"] ,

"comments" : [ { "$ref" : "comment" , "$id" : 1} , { "$ref" : "comment" , "$id" : 2}]}

I've tried to swap c1 and c2 in the code (for example use m.comments = [c2, c1]) and unexpectedly I've got the proper output:

2 message_two

1 message_one

Also I've tried to not use custom primary key "id" and in both cases everything worked properly.

I am very confused with this bug, it seems it's something wrong with mongoengine or I'm not using it in a right way. Please, any ideas?

like image 660
alex_smirnov Avatar asked Jul 17 '12 16:07

alex_smirnov


1 Answers

There was a bug in dereferencing and this was fixed in 0.6.15 - please upgrade!

like image 142
Ross Avatar answered Sep 23 '22 12:09

Ross