#35694: FK inconsistency beween `get_or_create` and any other orm function
-------------------------------------+-------------------------------------
     Reporter:  KIC                  |                    Owner:  (none)
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  5.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 KIC:

Old description:

> While any orm `filter`, `get`, ... accepts a `pk` or instance
> `get_or_create` does not. For consistency `get_or_create` should also be
> able to handle instances and pk's interchangeably.
>
> models.py:
> ```
> from django.db import models
>

> class A(models.Model):
>     foo = models.CharField(max_length=100)
>

> class B(models.Model):
>     a = models.ForeignKey(A, on_delete=models.CASCADE)
>     bar = models.CharField(max_length=100)
>
> ```
>
> tests.py
> ```
> from testing.utils import TestCase
> from zfoo import models
>

> class TestSomething(TestCase):
>     def test_lala(self):
>         a = models.A.objects.create(foo="foo")
>         models.B.objects.create(a=a, bar="bar")
>
>         self.assertEqual(models.B.objects.get(a=a),
> models.B.objects.get(a=a.pk))
>         self.assertEqual(
>             models.B.objects.filter(a=a).first(),
>             models.B.objects.filter(a=a.pk).first(),
>         )
>
>         models.B.objects.get_or_create(dict(bar="does not exist"),
> bar="baz", a=a)
>         models.B.objects.get_or_create(dict(bar="does not exist"),
> bar="baz", a=a.pk)  # FAILS
> ```

New description:

 While any orm `filter`, `get`, ... accepts a `pk` or instance
 `get_or_create` does not. For consistency `get_or_create` should also be
 able to handle instances and pk's interchangeably.


 models.py:
 {{{
 from django.db import models


 class A(models.Model):
     foo = models.CharField(max_length=100)


 class B(models.Model):
     a = models.ForeignKey(A, on_delete=models.CASCADE)
     bar = models.CharField(max_length=100)

 }}}

 tests.py
 {{{
 from testing.utils import TestCase
 from zfoo import models


 class TestSomething(TestCase):
     def test_lala(self):
         a = models.A.objects.create(foo="foo")
         models.B.objects.create(a=a, bar="bar")

         self.assertEqual(models.B.objects.get(a=a),
 models.B.objects.get(a=a.pk))
         self.assertEqual(
             models.B.objects.filter(a=a).first(),
             models.B.objects.filter(a=a.pk).first(),
         )

         models.B.objects.get_or_create(dict(bar="does not exist"),
 bar="baz", a=a)
         models.B.objects.get_or_create(dict(bar="does not exist"),
 bar="baz", a=a.pk)  # FAILS
 }}}

--
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35694#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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701916fb339ff-7c414157-f353-49bb-b822-b16aff9cccaa-000000%40eu-central-1.amazonses.com.

Reply via email to