#36235: RelatedManager.all().get_or_create() does not work
-------------------------------------+-------------------------------------
     Reporter:  Nick Pope            |                    Owner:  Johanan
                                     |  Oppong Amoateng
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  get_or_create,       |             Triage Stage:  Accepted
  related, manager                   |
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Vasilis Vagenas):

 Hello.
 The problem can still be reproduced:
 * Git commit hash: `e17ee4468875077b90b70bb6a589ebad7493f757`
 * test method: the method `test_get_or_create_on_related_queryset`,
 mentioned above by Nick Pope
 * test output:

 {{{

 ======================================================================
 ERROR: test_get_or_create_on_related_queryset
 (get_or_create.tests.GetOrCreateTests)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File
 "/home/vasilis/oss_projects/django/django/django/db/models/query.py", line
 581, in get_or_create
     return self.get(**kwargs), False
   File
 "/home/vasilis/oss_projects/django/django/django/db/models/query.py", line
 435, in get
     raise self.model.DoesNotExist(
 get_or_create.models.Book.DoesNotExist: Book matching query does not
 exist.

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File
 "/home/vasilis/oss_projects/django/django/django/db/backends/utils.py",
 line 84, in _execute
     return self.cursor.execute(sql, params)
   File
 "/home/vasilis/oss_projects/django/django/django/db/backends/sqlite3/base.py",
 line 416, in execute
     return Database.Cursor.execute(self, query, params)
 sqlite3.IntegrityError: NOT NULL constraint failed:
 get_or_create_book.publisher_id_column

 The above exception was the direct cause of the following exception:

 Traceback (most recent call last):
   File "/usr/lib/python3.10/unittest/case.py", line 59, in
 testPartExecutor
     yield
   File "/usr/lib/python3.10/unittest/case.py", line 591, in run
     self._callTestMethod(testMethod)
   File "/usr/lib/python3.10/unittest/case.py", line 549, in
 _callTestMethod
     method()
   File
 "/home/vasilis/oss_projects/django/django/tests/get_or_create/tests.py",
 line 143, in test_get_or_create_on_related_queryset
     book, created = p.books.all().get_or_create(name="The Book of Ed &
 Fred")
   File
 "/home/vasilis/oss_projects/django/django/django/db/models/query.py", line
 588, in get_or_create
     return self.create(**params), True
   File
 "/home/vasilis/oss_projects/django/django/django/db/models/query.py", line
 453, in create
     obj.save(force_insert=True, using=self.db)
   File
 "/home/vasilis/oss_projects/django/django/django/db/models/base.py", line
 726, in save
     self.save_base(using=using, force_insert=force_insert,
   File
 "/home/vasilis/oss_projects/django/django/django/db/models/base.py", line
 763, in save_base
     updated = self._save_table(
   File
 "/home/vasilis/oss_projects/django/django/django/db/models/base.py", line
 868, in _save_table
     results = self._do_insert(cls._base_manager, using, fields,
 returning_fields, raw)
   File
 "/home/vasilis/oss_projects/django/django/django/db/models/base.py", line
 906, in _do_insert
     return manager._insert(
   File
 "/home/vasilis/oss_projects/django/django/django/db/models/manager.py",
 line 85, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
   File
 "/home/vasilis/oss_projects/django/django/django/db/models/query.py", line
 1268, in _insert
     return query.get_compiler(using=using).execute_sql(returning_fields)
   File
 "/home/vasilis/oss_projects/django/django/django/db/models/sql/compiler.py",
 line 1401, in execute_sql
     cursor.execute(sql, params)
   File
 "/home/vasilis/oss_projects/django/django/django/db/backends/utils.py",
 line 66, in execute
     return self._execute_with_wrappers(sql, params, many=False,
 executor=self._execute)
   File
 "/home/vasilis/oss_projects/django/django/django/db/backends/utils.py",
 line 75, in _execute_with_wrappers
     return executor(sql, params, many, context)
   File
 "/home/vasilis/oss_projects/django/django/django/db/backends/utils.py",
 line 79, in _execute
     with self.db.wrap_database_errors:
   File "/home/vasilis/oss_projects/django/django/django/db/utils.py", line
 90, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File
 "/home/vasilis/oss_projects/django/django/django/db/backends/utils.py",
 line 84, in _execute
     return self.cursor.execute(sql, params)
   File
 "/home/vasilis/oss_projects/django/django/django/db/backends/sqlite3/base.py",
 line 416, in execute
     return Database.Cursor.execute(self, query, params)
 django.db.utils.IntegrityError: NOT NULL constraint failed:
 get_or_create_book.publisher_id_column

 ----------------------------------------------------------------------

 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36235#comment:12>
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 visit 
https://groups.google.com/d/msgid/django-updates/0107019d9fff129b-8b8e9346-5703-4e24-8df4-199c5a44fc7b-000000%40eu-central-1.amazonses.com.

Reply via email to