MongoEngine / MongoEngine/mongoengine

It seems like a bug. set unique=True make the field be required. and also the spare=True raise Error.

Open
#824 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

the code is below:

#!/usr/bin/env python
# encoding: utf-8

from mongoengine import Document, connect
from mongoengine import StringField


class Post(Document):
    title = StringField(required=True)
    content = StringField(unique=True)


if __name__ == "__main__":
    connect('test')

    p1 = Post()
    p1.title = "hello"
    p1.save()

and the result show like that:

 $ python mg_models.py 
Traceback (most recent call last):
  File "mg_models.py", line 18, in <module>
    p1.save()
  File "/usr/local/lib/python2.7/dist-packages/mongoengine/document.py", line 224, in save
    self.validate(clean=clean)
  File "/usr/local/lib/python2.7/dist-packages/mongoengine/base/document.py", line 323, in validate
    raise ValidationError(message, errors=errors)
mongoengine.errors.ValidationError: ValidationError (Post:None) (Field is required: ['content'])

this is my env:

 $ uname -a
Linux dp-pc 3.8.0-44-generic #66~precise1-Ubuntu SMP Tue Jul 15 04:01:04 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
 $ python --version
Python 2.7.3

 $ mongod --version
db version v2.6.3
2014-12-04T17:22:12.783+0800 git version: 255f67a66f9603c59380b2a389e386910bbb52cb

In [2]: mongoengine.VERSION
Out[2]: (0, 8, 7)

and also if the follow code:

#!/usr/bin/env python
# encoding: utf-8

from mongoengine import Document, connect
from mongoengine import StringField


class Post(Document):
    title = StringField(required=True)
    content = StringField(unique=True)
    auth = StringField(unique=True, sparse=True)


if __name__ == "__main__":
    connect('test')

    p1 = Post()
    p1.title = "hello"
    p1.save()

give the error:

  $ python mg_models.py 
Traceback (most recent call last):
  File "mg_models.py", line 8, in <module>
    class Post(Document):
  File "mg_models.py", line 11, in Post
    auth = StringField(unique=True, sparse=True)
  File "/usr/local/lib/python2.7/dist-packages/mongoengine/fields.py", line 60, in __init__
    super(StringField, self).__init__(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'sparse'

the doc has this, but I check the source has not such paramters.
1
2

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.

Research direction

Start by running the two Python reproductions against the versions listed in the issue. Inspect mongoengine/document.py, base/document.py, and fields.py, then compare their behavior with the referenced documentation. Done means the unique and sparse field behavior is consistent with the documented API and covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.