Opened 43 hours ago

Closed 28 hours ago

#36188 closed Bug (invalid)

Reverse lookups on OneToOneField not working?

Reported by: Willem Van Onsem Owned by: hesham hatem
Component: Database layer (models, ORM) Version: 5.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no
Pull Requests:How to create a pull request

Description

This StackOverflow post shows a scenario where a OneToOneField is filtered on top of another OneToOneField. So for example with the following modeling:

class Switch(models.Model):
    fqdn = models.CharField(max_length=45, unique=True)

class ConfigState(models.Model):
    switch = models.OneToOneField(Switch, models.CASCADE, db_column='switch', primary_key=True,
                                  related_name='config_state')

class EdgeSwitch(models.Model):
    switch = models.OneToOneField(Switch, models.CASCADE, db_column='switch', primary_key=True,
                                      related_name='edge_switch')

The query EdgeSwitch.objects.filter(switch__config_state=1) does (no longer) work. On Django-3.x it apparently worked.

Change History (5)

comment:1 by hesham hatem, 38 hours ago

Last edited 38 hours ago by hesham hatem (previous) (diff)

comment:2 by hesham hatem, 38 hours ago

Owner: set to hesham hatem
Status: newassigned

comment:3 by Simon Charette, 35 hours ago

Resolution: invalid
Status: assignedclosed

I cannot reproduce the issue against main or stable/5.1.x with the following patch based on the provided models

  • TabularUnified new file tests/ticket_36188/models.py

    diff --git a/tests/ticket_36188/__init__.py b/tests/ticket_36188/__init__.py
    new file mode 100644
    index 0000000000..e69de29bb2
    diff --git a/tests/ticket_36188/models.py b/tests/ticket_36188/models.py
    new file mode 100644
    index 0000000000..c6a6ee01bc
    - +  
     1from django.db import models
     2
     3
     4class Switch(models.Model):
     5    fqdn = models.CharField(max_length=45, unique=True)
     6
     7
     8class ConfigState(models.Model):
     9    switch = models.OneToOneField(
     10        Switch,
     11        models.CASCADE,
     12        db_column="switch",
     13        primary_key=True,
     14        related_name="config_state",
     15    )
     16
     17
     18class EdgeSwitch(models.Model):
     19    switch = models.OneToOneField(
     20        Switch,
     21        models.CASCADE,
     22        db_column="switch",
     23        primary_key=True,
     24        related_name="edge_switch",
     25    )
  • TabularUnified new file tests/ticket_36188/tests.py

    diff --git a/tests/ticket_36188/tests.py b/tests/ticket_36188/tests.py
    new file mode 100644
    index 0000000000..dbfdd6bb7b
    - +  
     1from django.test import TestCase
     2
     3from .models import EdgeSwitch
     4
     5
     6class Ticket36188Tests(TestCase):
     7    def test_reported_case(self):
     8        list(EdgeSwitch.objects.filter(switch__config_state=1))
     9        print(EdgeSwitch.objects.filter(switch__config_state=1).query)

Please re-open if you can provide a set of models that trigger a FieldError.

Last edited 35 hours ago by Simon Charette (previous) (diff)

comment:4 by willem-van-onsem, 30 hours ago

Resolution: invalid
Status: closednew

I found the missing part: the Switch model needs to have managed = False. A bit curious that this has impact, so:

class Switch(models.Model):
    fqdn = models.CharField(max_length=45, unique=True)
    
    class Meta:
        managed = False

comment:5 by willem-van-onsem, 28 hours ago

Resolution: invalid
Status: newclosed

I think I got it, the tables got an app_label, but that was probably not registered.

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