Unique indexes are not being used

XMLWordPrintableJSON

    • None
    • Python Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Dear Team,

      InĀ 

      In the following function `django_mongodb_backend.indexes.get_pymongo_index_model` we are adding the partial filter expression on type of the field if the index to be added is declared unique in the models file.

      if unique:
          kwargs["unique"] = True
          # Indexing on $type matches the value of most SQL databases by
          # allowing multiple null values for the unique constraint.
          if field:
              column = column_prefix + field.column
              filter_expression[column].update({"$type": field.db_type(schema_editor.connection)})
          else:
              for field_name, _ in self.fields_orders:
                  field_ = model._meta.get_field(field_name)
                  filter_expression[field_.column].update(
                      {"$type": field_.db_type(schema_editor.connection)}
                  )

      This results in making the index partial on Atlas and simple queries like `Book.objects.get(name='Mongo DB')` does not use the created index. I verified this on Atlas showing index usages as 0 and through query plans too. Remove the index and creating it manually on Atlas solved the problem but then I will have to do it everytime new migration is added with unique index or unique constraint.

      Am I doing something wrong while writing the queries or is it an expected issue?

            Assignee:
            Jib Adegunloye
            Reporter:
            Ritik Singla
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: