Opened 6 years ago

Closed 6 years ago

#30409 closed Cleanup/optimization (fixed)

E012 is raised when ForeignKey is used by "_id" in an Index.

Reported by: James Cheese Owned by: zeynel
Component: Core (System checks) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Looks to be an issue with the implementation in #28714 - issue found whilst upgrading from 2.0 to 2.2, but also occurs in latest 2.1.x.

To duplicate, create a compound index including the ID for a ForeignKey, eg:

class DataConcentrator(models.Model):
    #...

class ConcentratorHeartbeat(models.Model):
    #...
    class Meta:
        indexes = [
            models.Index(fields=["source_id", "-collected_at"]),
        ]
    #...
    source = models.ForeignKey(DataConcentrator, null=True, related_name="heartbeats", on_delete=models.CASCADE)

Expected behaviour:
No errors raised

Encountered behaviour:
Error raised on system check:

Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x00000298392F3D08>
Traceback (most recent call last):
  File "C:\Workspace\sensor-analytics-tool\.pyenv\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Workspace\sensor-analytics-tool\.pyenv\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "C:\Workspace\sensor-analytics-tool\.pyenv\lib\site-packages\django\core\management\base.py", line 425, in check
    raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
sensor_receiver.ConcentratorHeartbeat: (models.E012) 'indexes' refers to the nonexistent field 'source_id'.

System check identified 2 issues (0 silenced).

Current workaround is to just silence E012. Gets things running, but obviously not ideal.

Change History (7)

comment:1 by Mariusz Felisiak, 6 years ago

Summary: System check E012 erroneously raised when ForeignKey is used in compound indexE012 is raised when ForeignKey is used by "_id" in an Index.

I agree that this behavior has changed in Django 2.1, but currently check is consistent with similar checks for index_together and unique_together. To use foreign keys in models.Index you can use field name, i.e.

indexes = [
    models.Index(fields=["source", "-collected_at"]),
]

comment:2 by James Cheese, 6 years ago

That's fair - thanks for the clarification. Is it possible/worth dropping a note regarding the new system check into the 2.1 release notes, in case others hit the same issue? Admittedly, if nobody else has seen this after 6 months it's probably not a big deal, but I assume there'll be some other people jumping from the 1.11 LTS straight to 2.2 LTS that might encounter it.

comment:3 by Simon Charette, 6 years ago

Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

I think we should adjust the checks to allow both .name and .attname forms since the rest of the ORM allows both.

comment:4 by zeynel, 6 years ago

Owner: changed from nobody to zeynel
Status: newassigned

I too agree that we should allow .attname usage in index_together, unique_together and indexes, since it is allowed to use .attname in other places such as order_with_respect_to, ordering.

comment:5 by zeynel, 6 years ago

Has patch: set

comment:6 by Mariusz Felisiak, 6 years ago

Version: 2.2master

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 6485a5f4:

Fixed #30409 -- Allowed using foreign key's attnames in unique/index_together and Index's fields.

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