Opened 15 months ago

Closed 15 months ago

Last modified 3 months ago

#28597 closed Bug (fixed)

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

Reported by: Дилян Палаузов Owned by: felixxm
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 Changed 15 months ago by felixxm

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

comment:2 Changed 15 months ago by felixxm

Has patch: set

comment:3 Changed 15 months ago by felixxm

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

comment:4 Changed 15 months ago by felixxm

Component: UncategorizedDatabase layer (models, ORM)

comment:5 Changed 15 months ago by Tim Graham <timograham@…>

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 Changed 3 months ago by Phil Krylov

Cc: Phil Krylov added

Can someone backport this to 1.11, please?

comment:7 Changed 3 months ago by Simon Charette

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

comment:8 Changed 3 months ago by Дилян Палаузов

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

comment:9 Changed 3 months ago by Simon Charette

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 Changed 3 months ago by Phil Krylov

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