LiveServerTestCase - self.client.force_login() when called more than once returns django.db.utils.DatabaseError

106 views
Skip to first unread message

Deep Sukhwani

unread,
Mar 4, 2020, 8:19:57 AM3/4/20
to Django developers (Contributions to Django itself)
Hello,

This query is for Django version 1.9 and Python 3.5 with Django Rest Framework 3.5.4
(I know this version is outdated, but moving ahead of this version as of now is out of reach for the purpose of solving this query)

Exact Error observed:

    self.client.force_login(self.user)
 
File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/test/client.py", line 608, in force_login
   
self._login(user)
 
File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/test/client.py", line 621, in _login
    login
(request, user)
 
File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/contrib/auth/__init__.py", line 117, in login
    user_logged_in
.send(sender=user.__class__, request=request, user=user)
 
File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 192, in send
    response
= receiver(signal=self, sender=sender, **named)
 
File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/contrib/auth/models.py", line 23, in update_last_login
    user
.save(update_fields=['last_login'])
 
File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 74, in save
   
super(AbstractBaseUser, self).save(*args, **kwargs)
 
File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/db/models/base.py", line 708, in save
    force_update
=force_update, update_fields=update_fields)
 
File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/db/models/base.py", line 736, in save_base
    updated
= self._save_table(raw, cls, force_insert, force_update, using, update_fields)
 
File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/db/models/base.py", line 805, in _save_table
   
raise DatabaseError("Save with update_fields did not affect any rows.")
django
.db.utils.DatabaseError: Save with update_fields did not affect any rows.


Scenario:

from django.test import TestCase, LiveServerTestCase

from paths.factories import UserFactory


class ATestClass(LiveServerTestCase):

    @classmethod
    def setUpClass(cls):
        """
        Test Data common across all tests
        """

        super().setUpClass()

        cls.user = UserFactory(is_staff=False, is_superuser=False)

    def test_one(self):
        """
        Login and then do some actions
        """
        self.client.force_login(self.user)

        # do something
        self.assertTrue(True)

    def test_two(self):
        """
        Login and do some actions
        """

        self.client.force_login(self.user)

        # do something
        self.assertFalse(False)

However, if I change the base class from LiveServerTestCase to TestCase, it works fine without any errors. I understand this was probably addressed in https://code.djangoproject.com/ticket/26823 but can someone help with any way in which I can patch this in my tests to avoid raising this issue?

Why LiveServerTestCase?
  • My tests need access to live_server_url, hence I am forced to inherit the test class from LiveServerTestCase
What have I tried?
  • Logging in in setUp method instead of directly within the test - Doesn't help
  • Disconnecting update_last_login signal by using user_logged_in.disconnect(update_last_login) - Doesn't help
  • Manually setting the last_login field on user object to None before force_login statement - Doesn't help
Thank you,
Deep S

Adam Johnson

unread,
Mar 4, 2020, 8:37:48 AM3/4/20
to django-d...@googlegroups.com
Hi!

I'm pasting my "wrong mailing list" standard text below. This is a support query for an old version of Django.

The ticket you linked to was committed in https://github.com/django/django/commit/eedc88bd4a8468c2a0daa8346c9b57183dd77782 which I can see was released in Django 2.0.

You should really upgrade!

Thanks,

Adam

---

I think you've found the wrong mailing list for this post. This mailing list is for the development of Django itself, not for support using Django. This means the discussions of bugs and features in Django itself, rather than in your code using it. People on this list are unlikely to answer your support query with their limited time and energy. Read more on the mailing lists at https://www.djangoproject.com/community/

For support, please use the NEW Django forum at https://forum.djangoproject.com , django-users mailing list, or IRC #django on Freenode, or a site like Stack Overflow. There are people out there willing to help on those channels, but they might not respond if you don't ask your question well. Stack Overflow's question guide can help you frame it well: https://stackoverflow.com/help/how-to-ask .

Also if you haven't read it, please take a look at Django's Code of Conduct: https://www.djangoproject.com/conduct/ . These are our "ground rules" for working well as a community, and will help you get the most out of Django and our fantastic community.

Thanks for your understanding,

Adam

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/623f7119-d398-4254-b80d-ead27d8f4fbe%40googlegroups.com.


--
Adam

Deep Sukhwani

unread,
Mar 4, 2020, 8:45:48 AM3/4/20
to Django developers (Contributions to Django itself)
Thank you, Adam,

Note: For future posts on this topic - Request you to use django-users mailing list: https://groups.google.com/forum/#!topic/django-users/GbU7FekodGY

I realized that I posted this in the wrong mailing list after posting it and so I reposted this in django-users mailing list. Extremely sorry for this.

Thanks for calling out that we should really upgrade to Django 2.0, however, for now, I would love to understand if there is a way to patch this in Django 1.9 until we (my organization) are stuck on this version.

For anyone else, if you have a helpful response - request you to respond to it in the django-users mailing list - link to this post: https://groups.google.com/forum/#!topic/django-users/GbU7FekodGY

Thank you
To unsubscribe from this group and stop receiving emails from it, send an email to django-d...@googlegroups.com.


--
Adam

Adam Johnson

unread,
Mar 4, 2020, 10:08:17 AM3/4/20
to django-d...@googlegroups.com
I should have written: You can fork Django on GitHub, backport that commit (git cherry-pick) to the stable/1.9.x branch, and install that hash: https://adamj.eu/tech/2019/03/11/pip-install-from-a-git-repository/ .

To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/73ba4152-e6ef-42af-b7db-c58e3b0e7a15%40googlegroups.com.


--
Adam

Deep Sukhwani

unread,
Mar 4, 2020, 10:16:00 AM3/4/20
to django-d...@googlegroups.com
Thank you.

Oh, so this is what backporting means. In my attempts to understand Django internals, I have been browsing around several tickets for some weeks now and on some of them I read "Backporting <commit_sha>" wondering what that means 🙄

Thanks for helping me out in this regard too 🙂


Reply all
Reply to author
Forward
0 new messages