#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 , 8 years ago
| Owner: | changed from to |
|---|---|
| Severity: | Normal → Release blocker |
| Status: | new → assigned |
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 8 years ago
| Has patch: | set |
|---|
comment:3 by , 8 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 , 8 years ago
| Component: | Uncategorized → Database layer (models, ORM) |
|---|
comment:9 by , 7 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 , 7 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.
PR