Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Case insensitive uniqueness #263

Closed

Conversation

wojcikstefan
Copy link
Member

from mongoengine import *

connect('dbname')

class A(Document):
    b = IUniqueStringField()

c = A()
c.b = 'whatever'
c.save()

d = A()
d.b = 'WhatEVER'
d.save()

results in:

(env)[mongoengine] python test.py                                                                                                                             19:00:59 git:(master*)
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    c.save()
  File "/Users/wojcikstefan/Repos/mongoengine/mongoengine/document.py", line 266, in save
    raise NotUniqueError(message % unicode(err))
mongoengine.queryset.NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error index: tumblelog.a.$_b_lower_1  dup key: { : "whatever" })

@rozza
Copy link
Contributor

rozza commented Apr 11, 2013

Not 100% sold on this one - Maybe I'd be happier if it was a custom field - then we could be more explicit about the costs of this approach.

@wojcikstefan
Copy link
Member Author

@rozza I changed it to be a custom field. How do you like it now?

@rozza
Copy link
Contributor

rozza commented May 1, 2013

Looks good! I think this should go into: https://github.com/MongoEngine/extras-mongoengine

The fields themselves need docs and then we need a generic way to hook into mongoengine/base.py and do modifications there.

What do you think?

@wojcikstefan
Copy link
Member Author

Hey Ross! You're right, extras-mongoengine looks like a more suitable place for these fields. Feel free to close this PR, I'll make a new one in the other repo.

@wojcikstefan
Copy link
Member Author

Having given it more thought, I'm afraid that moving these changes to the other repo is non-trivial. I'd have to override the original DocumentMetaclass in extras-mongoengine and then you'd need to specify my_metaclass and __metaclass__ in the document, in which you'd wanna use the above fields. Seems like a big hassle.

Am I not seeing an obvious, easier solution?

@rozza
Copy link
Contributor

rozza commented May 2, 2013

I think we'd need to include some sort of hook in mongoengine to call from the field class. Let me ponder it :)

@wojcikstefan
Copy link
Member Author

@rozza, just following up - how's the pondering going? :) I would like to implement something like this in one of my projects very soon and am wondering whether to wait for the upstream changes or change my fork.

@wojcikstefan
Copy link
Member Author

Came up with a simpler implementation that can be easily included in https://github.com/MongoEngine/extras-mongoengine. I'll open a PR there soon.

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling 80cbe9d on elasticsales:case-insensitive-uniqueness into * on MongoEngine:master*.

@wojcikstefan wojcikstefan deleted the case-insensitive-uniqueness branch May 5, 2016 00:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants