#29896: Regression when saving a ForeignKey with to_field attr. Cached model is
lost.
-------------------------------------+-------------------------------------
Reporter: Maciej Gol | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
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
-------------------------------------+-------------------------------------
Description changed by Maciej Gol:
Old description:
> Hey guys, during recent patching run I've encountered regression between
> Django 2.0.9 and 2.1.2.
>
> Given models:
>
> {{{
> class Author(models.Model):
> username = models.CharField(primary_key=True)
> numeric_id = models.AutoField()
>
> class Book(models.Model):
> author = models.ForeignKey(Author, on_delete=models.Cascade)
> author_numeric = models.ForeignKey(Author, on_delete=models.Cascade,
> to_field="numeric_id")
> }}}
>
> The following test fails, although it shouldn't.
>
> {{{
> class SomeTest(TestCase):
> def
> test_foreign_key_with_to_field_should_properly_persist_through_save(self):
> author = Author.objects.create(username="yada")
> book = Book(author=author, author_numeric=author)
> book.save()
>
> with self.assertNumQueries(0):
> book.author_numeric # No queries below Django 2.1. One query
> for Django 2.1.
> }}}
>
> The problem comes from the `save()` method here:
>
> {{{
> # If the relationship's pk was changed, clear the cached
> # relationship.
> if obj and obj.pk != getattr(self, field.attname):
> field.delete_cached_value(self)
> }}}
>
> We compare `obj.pk` with `book.author_numeric_id`, but `pk` is a string
> (`username`) and `author_numeric_id` is an integer. It should look for
> `to_field` value, instead:
>
> {{{
> # If the relationship's pk was changed, clear the cached
> # relationship.
> if obj and getattr(obj, field.target_field.attname) !=
> getattr(self, field.attname):
> field.delete_cached_value(self)
> }}}
New description:
Hey guys, during recent patching run I've encountered regression between
Django 2.0.9 and 2.1.2.
Given models:
{{{
class Author(models.Model):
username = models.CharField(primary_key=True)
numeric_id = models.AutoField()
class Book(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE)
author_numeric = models.ForeignKey(Author, on_delete=models.CASCADE,
to_field="numeric_id")
}}}
The following test fails, although it shouldn't.
{{{
class SomeTest(TestCase):
def
test_foreign_key_with_to_field_should_properly_persist_through_save(self):
author = Author.objects.create(username="yada")
book = Book(author=author, author_numeric=author)
book.save()
with self.assertNumQueries(0):
book.author_numeric # No queries below Django 2.1. One query
for Django 2.1.
}}}
The problem comes from the `save()` method here:
{{{
# If the relationship's pk was changed, clear the cached
# relationship.
if obj and obj.pk != getattr(self, field.attname):
field.delete_cached_value(self)
}}}
We compare `obj.pk` with `book.author_numeric_id`, but `pk` is a string
(`username`) and `author_numeric_id` is an integer. It should look for
`to_field` value, instead:
{{{
# If the relationship's pk was changed, clear the cached
# relationship.
if obj and getattr(obj, field.target_field.attname) !=
getattr(self, field.attname):
field.delete_cached_value(self)
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/29896#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/068.c4943b82f56f45de48b8c1137ae7f8d7%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.