# Upgrading from 2.2 to 4.2 yields "RelatedObjectDoesNotExist - User has no account." error

**URL:** <https://forum.djangoproject.com/t/upgrading-from-2-2-to-4-2-yields-relatedobjectdoesnotexist-user-has-no-account-error/20437>\
**Category:** Mystery Errors\
**Created:** [April 21, 2023, 11:57pm UTC](https://forum.djangoproject.com/t/upgrading-from-2-2-to-4-2-yields-relatedobjectdoesnotexist-user-has-no-account-error/20437 "2023-04-21T23:57:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![toraritte](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/toraritte/32/11754_2.png) [@toraritte](https://forum.djangoproject.com/u/toraritte)\
**Post date:** [April 21, 2023, 11:57pm UTC](https://forum.djangoproject.com/t/upgrading-from-2-2-to-4-2-yields-relatedobjectdoesnotexist-user-has-no-account-error/20437/1 "2023-04-21T23:57:13Z")

</div>

Inherited a Django app running production that hasn’t been touched since 2020 (Django 2.2 + outdated dependencies, PosgreSQL 10.2), so I took the codebase, updated everything, and the only part of the code I touched was `settings.py`. Managed to make it run, logged in with a dummy account added via `manage.py createsuperuser`, and everything works.

At this point, I decided to do a `pg_dump` in production, replayed it on the dev side, (partially) applied the migrations, served it, and then tried to log in with my user that I use on the production version, but I get the following error:

```plaintext
django.contrib.auth.models.User.account.RelatedObjectDoesNotExist: User has no account.

```

I figured out that my user in the `auth_user` table has no corresponding entry in the `account_account` table using the SQL statement below:

```sql
SELECT * FROM account_account AS a JOIN auth_user AS u ON a.user_id = u.id;

```

The problem is that this is true for the app running in production as well and yet it still works. Now, I was able to get it to work by simply adding a corresponding entry to `account_account`,

```sql
SELECT id FROM auth_user WHERE username = 'my_username'; --> 27
INSERT INTO account_account(id, timezone, language, user_id) 
  VALUES (107,'','en', 27);

```

but even though we don’t have many users, this won’t be fun to do with each of them. (Um, should I just repeat this with every user? Should this be a migration? ) Anyway, at this point I’m just curious where (i.e., which version) this change in behaviour was introduced.

(I’m learning SQL, PostgreSQL, Python, and Django together on this “mission”, and had quite a ride so far even just getting things up to date. Nonetheless, I have tremendous respect for all of you making this framework so user friendly and the documentation is great. Thank you!)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 22, 2023, 12:46am UTC](https://forum.djangoproject.com/t/upgrading-from-2-2-to-4-2-yields-relatedobjectdoesnotexist-user-has-no-account-error/20437/2 "2023-04-22T00:46:38Z")

</div>

There is no model named `Account` that is part of Django itself - it must be part of the application. You should find out where it’s defined and how it relates to User. If it defines a OneToOne with User, that may help explain the behavior you are seeing.

Figuring out exactly what is happening would entail a deeper dive into the original environment to see how it is mapping to the current version.

---

<div class="post-metadata">

**Author:** ![toraritte](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/toraritte/32/11754_2.png) [@toraritte](https://forum.djangoproject.com/u/toraritte)\
**Post date:** [April 23, 2023, 1:08am UTC](https://forum.djangoproject.com/t/upgrading-from-2-2-to-4-2-yields-relatedobjectdoesnotexist-user-has-no-account-error/20437/3 "2023-04-23T01:08:14Z")

</div>

Thank you!

After doing some digging:

- the `auth_user` table is from the [Django repo’s `contrib/auth`](https://github.com/django/django/tree/main/django/contrib/auth)
- the `account_account` table is created by the [`django-user-accounts`](https://pypi.org/project/django-user-accounts/) package

---

<div class="post-metadata">

**Author:** ![toraritte](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/toraritte/32/11754_2.png) [@toraritte](https://forum.djangoproject.com/u/toraritte)\
**Post date:** [April 23, 2023, 1:09am UTC](https://forum.djangoproject.com/t/upgrading-from-2-2-to-4-2-yields-relatedobjectdoesnotexist-user-has-no-account-error/20437/4 "2023-04-23T01:09:49Z")

</div>

… and also just updated a 2021 issue in the [`django-user-accounts` repo](https://github.com/pinax/django-user-accounts/) regarding the same error ([#345](https://github.com/pinax/django-user-accounts/issues/345)).

(Sorry, had to cut this reply in two because of the “new user restrictions” I couldn’t post more than 2 links in one reply.)
