Merge branch 'master' into bump_pymongo_version_requirement
This commit is contained in:
commit
b70ffc69df
1
AUTHORS
1
AUTHORS
@ -249,3 +249,4 @@ that much better:
|
|||||||
* Bastien Gérard (https://github.com/bagerard)
|
* Bastien Gérard (https://github.com/bagerard)
|
||||||
* Trevor Hall (https://github.com/tjhall13)
|
* Trevor Hall (https://github.com/tjhall13)
|
||||||
* Gleb Voropaev (https://github.com/buggyspace)
|
* Gleb Voropaev (https://github.com/buggyspace)
|
||||||
|
* Paulo Amaral (https://github.com/pauloAmaral)
|
||||||
|
@ -5,6 +5,7 @@ Changelog
|
|||||||
Development
|
Development
|
||||||
===========
|
===========
|
||||||
- mongoengine now requires pymongo>=3.5 #2017
|
- mongoengine now requires pymongo>=3.5 #2017
|
||||||
|
- Generate Unique Indices for SortedListField and EmbeddedDocumentListFields #2020
|
||||||
- (Fill this out as you fix issues and develop your features).
|
- (Fill this out as you fix issues and develop your features).
|
||||||
|
|
||||||
Changes in 0.17.0
|
Changes in 0.17.0
|
||||||
|
@ -883,7 +883,8 @@ class BaseDocument(object):
|
|||||||
index = {'fields': fields, 'unique': True, 'sparse': sparse}
|
index = {'fields': fields, 'unique': True, 'sparse': sparse}
|
||||||
unique_indexes.append(index)
|
unique_indexes.append(index)
|
||||||
|
|
||||||
if field.__class__.__name__ == 'ListField':
|
if field.__class__.__name__ in {'EmbeddedDocumentListField',
|
||||||
|
'ListField', 'SortedListField'}:
|
||||||
field = field.field
|
field = field.field
|
||||||
|
|
||||||
# Grab any embedded document field unique indexes
|
# Grab any embedded document field unique indexes
|
||||||
|
@ -704,6 +704,77 @@ class IndexesTest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertRaises(NotUniqueError, post2.save)
|
self.assertRaises(NotUniqueError, post2.save)
|
||||||
|
|
||||||
|
def test_unique_embedded_document_in_sorted_list(self):
|
||||||
|
"""
|
||||||
|
Ensure that the uniqueness constraints are applied to fields in
|
||||||
|
embedded documents, even when the embedded documents in a sorted list
|
||||||
|
field.
|
||||||
|
"""
|
||||||
|
class SubDocument(EmbeddedDocument):
|
||||||
|
year = IntField()
|
||||||
|
slug = StringField(unique=True)
|
||||||
|
|
||||||
|
class BlogPost(Document):
|
||||||
|
title = StringField()
|
||||||
|
subs = SortedListField(EmbeddedDocumentField(SubDocument),
|
||||||
|
ordering='year')
|
||||||
|
|
||||||
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
|
post1 = BlogPost(
|
||||||
|
title='test1', subs=[
|
||||||
|
SubDocument(year=2009, slug='conflict'),
|
||||||
|
SubDocument(year=2009, slug='conflict')
|
||||||
|
]
|
||||||
|
)
|
||||||
|
post1.save()
|
||||||
|
|
||||||
|
# confirm that the unique index is created
|
||||||
|
indexes = BlogPost._get_collection().index_information()
|
||||||
|
self.assertIn('subs.slug_1', indexes)
|
||||||
|
self.assertTrue(indexes['subs.slug_1']['unique'])
|
||||||
|
|
||||||
|
post2 = BlogPost(
|
||||||
|
title='test2', subs=[SubDocument(year=2014, slug='conflict')]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertRaises(NotUniqueError, post2.save)
|
||||||
|
|
||||||
|
def test_unique_embedded_document_in_embedded_document_list(self):
|
||||||
|
"""
|
||||||
|
Ensure that the uniqueness constraints are applied to fields in
|
||||||
|
embedded documents, even when the embedded documents in an embedded
|
||||||
|
list field.
|
||||||
|
"""
|
||||||
|
class SubDocument(EmbeddedDocument):
|
||||||
|
year = IntField()
|
||||||
|
slug = StringField(unique=True)
|
||||||
|
|
||||||
|
class BlogPost(Document):
|
||||||
|
title = StringField()
|
||||||
|
subs = EmbeddedDocumentListField(SubDocument)
|
||||||
|
|
||||||
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
|
post1 = BlogPost(
|
||||||
|
title='test1', subs=[
|
||||||
|
SubDocument(year=2009, slug='conflict'),
|
||||||
|
SubDocument(year=2009, slug='conflict')
|
||||||
|
]
|
||||||
|
)
|
||||||
|
post1.save()
|
||||||
|
|
||||||
|
# confirm that the unique index is created
|
||||||
|
indexes = BlogPost._get_collection().index_information()
|
||||||
|
self.assertIn('subs.slug_1', indexes)
|
||||||
|
self.assertTrue(indexes['subs.slug_1']['unique'])
|
||||||
|
|
||||||
|
post2 = BlogPost(
|
||||||
|
title='test2', subs=[SubDocument(year=2014, slug='conflict')]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertRaises(NotUniqueError, post2.save)
|
||||||
|
|
||||||
def test_unique_with_embedded_document_and_embedded_unique(self):
|
def test_unique_with_embedded_document_and_embedded_unique(self):
|
||||||
"""Ensure that uniqueness constraints are applied to fields on
|
"""Ensure that uniqueness constraints are applied to fields on
|
||||||
embedded documents. And work with unique_with as well.
|
embedded documents. And work with unique_with as well.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user