[Django] #34198: Django content type prefetching wrong data

7 views
Skip to first unread message

Django

unread,
Dec 3, 2022, 6:12:21 AM12/3/22
to django-...@googlegroups.com
#34198: Django content type prefetching wrong data
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
MahavirAce |
Type: Bug | Status: new
Component: Database | Version: 4.1
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
{{{
class Base1(models.Model):
value = models.CharField(max_length=20)


class Base2(models.Model):
value = models.CharField(max_length=20)


class LinkedWithBase1(models.Model):
f = models.ForeignKey(Foreign, on_delete=models.PROTECT,
related_name="a_objects")


class LinkedWithBase2(models.Model):
f = models.ForeignKey(OtherForeign, on_delete=models.PROTECT,
related_name="b_objects")


class Connection(models.Model):
ct = models.ForeignKey(ContentType, on_delete=models.PROTECT)
object_id = models.IntegerField()
object = GenericForeignKey(ct_field='ct', fk_field='object_id')
}}}

**The data presentation :-**

1) Base1 Model

|| id || value||
|| 1|| Foreign||

2) Base2 Model

|| id || value||
|| 1|| OtherForeign||

3) LinkedWithBase1 model
|| id || f_id (FK)||
|| 1|| 1||

4) LinkedWithBase2 model
|| id || f_id (FK)||
|| 1|| 1||

Let's assume **LinkedWithBase1** model content_type id is **1** and
**LinkedWithBase2** content_type id is **2**.

Connection model
|| id || ct_id || object_id ||
|| 1 || 1 || 1||
|| 2 || 2 || 1||

**1 row is connected with id 1 of LinkedWithBase1 model
2 row is connected with id 1 of LinkedWithBase2 model**

When i was run below code

{{{
connections = Connection.objects.prefetch_related("object__f").all()

for i in connections:
print(i.object.f.value)
}}}


**Actual Output**
Foreign
Foreign

**Expected Output**
Foreign
OtherForeign

In 1st iteration **i.object.f** evaluate **Base1 model instance** which is
right
In 2nd iteration **i.object.f** it should be take **Base2 model instance**
but django takes as **Base1 model instance**
because of wrong prefetching occurs

But when i removed **f** from prefetch_related it gives me a expected
result

Ex: connections = Connection.objects.prefetch_related("object").all()

i am very confident prefetching went wrong because i spent 5 hours to
debug this issue in Django core and i got it doing wrong prefetching when
it has a multiple content types as same name of key like **f** attribute
in above code, I have patch for this.

I will very greatful if you give me a chance to fix this issue

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

Django

unread,
Dec 3, 2022, 6:19:37 AM12/3/22
to django-...@googlegroups.com
#34198: Django content type prefetching wrong data
-------------------------------------+-------------------------------------
Reporter: Mahavir Agrawal | Owner: Mahavir
| Agrawal
Type: Bug | Status: assigned
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

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

* owner: nobody => Mahavir Agrawal
* status: new => assigned


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

Django

unread,
Dec 3, 2022, 8:34:12 AM12/3/22
to django-...@googlegroups.com
#34198: Django content type prefetching wrong data
-------------------------------------+-------------------------------------
Reporter: Mahavir Agrawal | Owner: Mahavir
| Agrawal
Type: Bug | Status: closed

Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

* status: assigned => closed
* has_patch: 1 => 0
* resolution: => duplicate


Comment:

Duplicate of #34178 and #33651.

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

Reply all
Reply to author
Forward
0 new messages