Opened 7 years ago

Closed 7 years ago

Last modified 6 years ago

#28597 closed Bug (fixed)

Class-based model indexes containing default primary key doesn't work.

Reported by: Дилян Палаузов Owned by: Mariusz Felisiak
Component: Database layer (models, ORM) Version: 1.11
Severity: Release blocker Keywords: Model.Meta.indexes
Cc: Phil Krylov Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This works:

class Z(models.Model)
  a = models.BooleanField()
  class Meta:
   index_together = ['a', 'id']`

but this does not:

  class Meta:
    indexes = [ models.Index(fields=['a', 'id']) ]
 File "d/models.py", line 589, in <module>
    class Z(models.Model):
  File "django/lib/python3.5/site-packages/django/db/models/base.py", line 314, in __new__
    index.set_name_with_model(new_class)
  File "django/lib/python3.5/site-packages/django/db/models/indexes.py", line 105, in set_name_with_model
    column_names = [model._meta.get_field(field_name).column for field_name, order in self.fields_orders]
  File "django/lib/python3.5/site-packages/django/db/models/indexes.py", line 105, in <listcomp>
    column_names = [model._meta.get_field(field_name).column for field_name, order in self.fields_orders]
  File "django/lib/python3.5/site-packages/django/db/models/options.py", line 611, in get_field
    "be available yet." % (self.object_name, field_name)
django.core.exceptions.FieldDoesNotExist: Z has no field named 'id'. The app cache isn't ready yet, so if this is an auto-created related field, it won't be available yet.

Using 'pk' instead does not help.

Providing, that "The newer indexes option provides more functionality than index_together. index_together may be deprecated in the future", how shall I deal with indexes= including the 'id' ?

Change History (10)

comment:1 by Mariusz Felisiak, 7 years ago

Owner: changed from nobody to Mariusz Felisiak
Severity: NormalRelease blocker
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:2 by Mariusz Felisiak, 7 years ago

Has patch: set

comment:3 by Mariusz Felisiak, 7 years ago

Summary: Model.Meta.indexes : model has no field named 'id'Class-based model indexes containing default primary key doesn't work.

comment:4 by Mariusz Felisiak, 7 years ago

Component: UncategorizedDatabase layer (models, ORM)

comment:5 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In fb02ebe:

Fixed #28597 -- Fixed crash with the name of a model's autogenerated primary key in an Index's fields.

comment:6 by Phil Krylov, 6 years ago

Cc: Phil Krylov added

Can someone backport this to 1.11, please?

comment:7 by Simon Charette, 6 years ago

This won't be backported to 1.11 per our backport policy.

comment:8 by Дилян Палаузов, 6 years ago

The correction is included in Django 1.11.6, see the release notes.

comment:9 by Simon Charette, 6 years ago

Hmm it's weird that the backport wasn't reported back here like the commit against master was.

Here it is for reference cee07ba0880e9d1396795e4823a7371da90cc5cc

comment:10 by Phil Krylov, 6 years ago

Thanks! For some reason I am still getting this error on 1.11.9. I'll recheck.

Note: See TracTickets for help on using tickets.
Back to Top