MongoEngine / MongoEngine/mongoengine

first() returns no result with certain data structures

Open
#840 3 comments 0 reactions 1 assignee View on GitHub

@MRigal is already working on this.

Since Apr 29, 2015.

Enhancement
Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

This one took me a while to track down. It's fairly mysterious, as it only shows up with very particular data structures. Below is a quick script that you can run in the python shell as an example:

import mongoengine
import uuid

mongoengine.connect("test-db")

class TestDocument(mongoengine.Document):
    tech = mongoengine.fields.UUIDField(required=True, unique_with='name', binary=False)
    name = mongoengine.fields.StringField(required=True)
    public = mongoengine.fields.BooleanField(required=True, default=True)
    definition = mongoengine.fields.DictField(required=True)
    default = mongoengine.fields.DynamicField(required=True)

data = {
    'tech': uuid.UUID('ec3d9a97-bee0-4218-88ee-8e500ae7b11e'),
    'name': 'tools',
    'definition': {
        'type': 'object'
    }, 
    'default': {
        "name": "PLA",
        "print_temperature_c": 220,
        "bed_temperature_c": 120,
    }
}

ta = TestDocument(**data)
ta.save()
print TestDocument.objects(**data).count()
print TestDocument.objects(**data).all()
print TestDocument.objects(**data).first()

The issue is that count() returns 1, all() returns a list of length 1, and first returns None. I looked into the source, and I believe it may be related to BaseQuerySet.__getitem__, but I got stymied around line 150.

I would classify this as a fairly urgent bug, as it's a public part of the API that fails silently and mysteriously.

Thanks for the great wrapper by the way - interfaces with Django pretty well.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.