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

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:

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:

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,

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!)

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.

Thank you!

After doing some digging:

… and also just updated a 2021 issue in the django-user-accounts repo regarding the same error (#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.)