Skip to content

Pymongo throwing an exception when changing index #1180

Description

@Scipi

When updating an index through Eve with mongo_indexes, if the keys in the index changes after the index was already created, pymongo will throw an exception instead of Eve dropping the existing index and recreating it as claimed.


Expected Behavior

From http://python-eve.org/config.html

Also remember that, by default, any already existent index for which the definition has been changed, will be dropped and re-created

In my settings.py

DOMAIN = {
  'users': {
    'schema': {'username': {'type': 'string'}, 'token': {'type': 'string'}},
    'mongo_indexes': {'my_index': [('username', 1)]}
  }
}
# Run the app, the collection with the index gets created fine
# Later, we change the index here to
DOMAIN = {
  'users': {
    'schema': {'username': {'type': 'string'}, 'token': {'type': 'string'}},
    'mongo_indexes': {'my_index': [('username', 1), ('token', 1)]}
  }
}

# And run the app again

Actual Behavior

The existing index does not get dropped and recreated. Instead, pymongo throws an exception since it attempts to create another index with the same name but different keys

Traceback (most recent call last):
  File "run.py", line 2, in <module>
    app = Eve()
  File "/home/dev/envs/medscan3.6/lib/python3.5/site-packages/eve/flaskapp.py", line 178, in __init__
    self.register_resource(resource, settings)
  File "/home/dev/envs/medscan3.6/lib/python3.5/site-packages/eve/flaskapp.py", line 932, in register_resource
    ensure_mongo_indexes(self, resource)
  File "/home/dev/envs/medscan3.6/lib/python3.5/site-packages/eve/io/mongo/mongo.py", line 969, in ensure_mongo_indexes
    _create_index(app, resource, name, list_of_keys, index_options)
  File "/home/dev/envs/medscan3.6/lib/python3.5/site-packages/eve/io/mongo/mongo.py", line 1020, in _create_index
    coll.create_index(list_of_keys, **kw)
  File "/home/dev/envs/medscan3.6/lib/python3.5/site-packages/pymongo/collection.py", line 1754, in create_index
    self.__create_index(keys, kwargs, session, **cmd_options)
  File "/home/dev/envs/medscan3.6/lib/python3.5/site-packages/pymongo/collection.py", line 1656, in __create_index
    session=session)
  File "/home/dev/envs/medscan3.6/lib/python3.5/site-packages/pymongo/collection.py", line 245, in _command
    retryable_write=retryable_write)
  File "/home/dev/envs/medscan3.6/lib/python3.5/site-packages/pymongo/pool.py", line 517, in command
    collation=collation)
  File "/home/dev/envs/medscan3.6/lib/python3.5/site-packages/pymongo/network.py", line 125, in command
    parse_write_concern_error=parse_write_concern_error)
  File "/home/dev/envs/medscan3.6/lib/python3.5/site-packages/pymongo/helpers.py", line 145, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Index must have unique name.The existing index: { v: 2, key: { username: 1 }, name: "my_index", ns: "eve.users" } has the same name as the requested index: { v: 2, key: { username: 1, token: 1 }, name: "my_index", ns: "eve.users" }

Additional Info

I believe this is because the code Eve uses to recreate the index expects an error code from pymongo to be 85
https://github.com/pyeve/eve/blob/master/eve/io/mongo/mongo.py#L1059

A quick test shows the exception I receive has an error code of 86, which according to mongo's source represents IndexKeySpecsConflict (85 is IndexOptionsConflict)

https://github.com/mongodb/mongo/blob/master/src/mongo/base/error_codes.err#L87

Environment

  • Python version: 3.5
  • Eve version: 0.8
  • Pymongo version: 3.6.1
  • Mongo version: 3.6.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions