[Django] #30409: System check E012 erroneously raised when ForeignKey is used in compound index

14 views
Skip to first unread message

Django

unread,
Apr 26, 2019, 7:27:48 AM4/26/19
to django-...@googlegroups.com
#30409: System check E012 erroneously raised when ForeignKey is used in compound
index
------------------------------------------------+------------------------
Reporter: tr00st | Owner: nobody
Type: Bug | Status: new
Component: Core (System checks) | Version: 2.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
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:

{{{#!python

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/30409>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 26, 2019, 8:27:05 AM4/26/19
to django-...@googlegroups.com
#30409: E012 is raised when ForeignKey is used by "_id" in an Index.
-------------------------------------+-------------------------------------
Reporter: James Cheese | Owner: nobody
Type: Bug | Status: new
Component: Core (System | Version: 2.2
checks) |
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by felixxm):

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"]),
]
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30409#comment:1>

Django

unread,
Apr 26, 2019, 8:43:15 AM4/26/19
to django-...@googlegroups.com
#30409: E012 is raised when ForeignKey is used by "_id" in an Index.
-------------------------------------+-------------------------------------
Reporter: James Cheese | Owner: nobody
Type: Bug | Status: new

Component: Core (System | Version: 2.2
checks) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by James Cheese):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/30409#comment:2>

Django

unread,
Apr 26, 2019, 10:25:05 AM4/26/19
to django-...@googlegroups.com
#30409: E012 is raised when ForeignKey is used by "_id" in an Index.
--------------------------------------+------------------------------------

Reporter: James Cheese | Owner: nobody
Type: Cleanup/optimization | Status: new

Component: Core (System checks) | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Simon Charette):

* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/30409#comment:3>

Django

unread,
Apr 29, 2019, 9:19:26 AM4/29/19
to django-...@googlegroups.com
#30409: E012 is raised when ForeignKey is used by "_id" in an Index.
--------------------------------------+------------------------------------
Reporter: James Cheese | Owner: zeynel
Type: Cleanup/optimization | Status: assigned

Component: Core (System checks) | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by zeynel):

* status: new => assigned
* owner: nobody => zeynel


Comment:

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`.

--
Ticket URL: <https://code.djangoproject.com/ticket/30409#comment:4>

Django

unread,
Apr 30, 2019, 6:11:40 AM4/30/19
to django-...@googlegroups.com
#30409: E012 is raised when ForeignKey is used by "_id" in an Index.
--------------------------------------+------------------------------------
Reporter: James Cheese | Owner: zeynel
Type: Cleanup/optimization | Status: assigned
Component: Core (System checks) | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by zeynel):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/30409#comment:5>

Django

unread,
Apr 30, 2019, 12:08:16 PM4/30/19
to django-...@googlegroups.com
#30409: E012 is raised when ForeignKey is used by "_id" in an Index.
--------------------------------------+------------------------------------
Reporter: James Cheese | Owner: zeynel
Type: Cleanup/optimization | Status: assigned
Component: Core (System checks) | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by felixxm):

* version: 2.2 => master


--
Ticket URL: <https://code.djangoproject.com/ticket/30409#comment:6>

Django

unread,
May 1, 2019, 5:52:57 AM5/1/19
to django-...@googlegroups.com
#30409: E012 is raised when ForeignKey is used by "_id" in an Index.
--------------------------------------+------------------------------------
Reporter: James Cheese | Owner: zeynel
Type: Cleanup/optimization | Status: closed
Component: Core (System checks) | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"6485a5f450b3dc60e690c31a75e0e9574a896842" 6485a5f4]:
{{{
#!CommitTicketReference repository=""
revision="6485a5f450b3dc60e690c31a75e0e9574a896842"
Fixed #30409 -- Allowed using foreign key's attnames in
unique/index_together and Index's fields.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30409#comment:7>

Reply all
Reply to author
Forward
0 new messages