Home > Net >  ValueError: Related model 'auth.user' cannot be resolved while migrating
ValueError: Related model 'auth.user' cannot be resolved while migrating

Time:02-04

I am trying to configure a project for pythonanywhere.com.

I deleted an old sqlite3 file, as well as migrations, so now I want to restore it. I launched command py manage.py makemigrations, then I run py manage.py migrate. But there I got an error:

Operations to perform:
  Apply all migrations: FriendsAnalyzer, account, admin, auth, contenttypes, sessions, sites, socialaccount
Running migrations:
  Applying socialaccount.0001_initial...Traceback (most recent call last):
  File "/home/FarmerKarwer/VkAnalyzer/manage.py", line 22, in <module>
    main()
  File "/home/FarmerKarwer/VkAnalyzer/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/core/management/base.py", line 96, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 349, in handle
    post_migrate_state = executor.migrate(
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/migrations/migration.py", line 130, in apply
    operation.database_forwards(
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/migrations/operations/models.py", line 96, in database_forwards
    schema_editor.create_model(model)
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 444, in create_model
    sql, params = self.table_sql(model)
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 216, in table_sql
    definition, extra_params = self.column_sql(model, field)
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 348, in column_sql
    field_db_params = field.db_parameters(connection=self.connection)
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/models/fields/related.py", line 1183, in db_parameters
    target_db_parameters = self.target_field.db_parameters(connection)
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/models/fields/related.py", line 1060, in target_field
    return self.foreign_related_fields[0]
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/models/fields/related.py", line 747, in foreign_related_fields
    rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/models/fields/related.py", line 734, in related_fields
    return self.resolve_related_fields()
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/models/fields/related.py", line 1103, in resolve_related_fields
    related_fields = super().resolve_related_fields()
  File "/home/FarmerKarwer/.virtualenvs/digiholmes-virtualenv/lib/python3.9/site-packages/django/db/models/fields/related.py", line 712, in resolve_related_fields
    raise ValueError(
ValueError: Related model 'auth.user' cannot be resolved

I don't think that the problem is in models.py because it worked well on my local computer. But here it is:

from django.db import models
from allauth.socialaccount.models import SocialAccount

# Create your models here.
class ResultInfo(models.Model):
    uid = models.ForeignKey(SocialAccount, on_delete=models.CASCADE)
    friend_id = models.CharField(max_length = 16)
    status = models.BooleanField()
    status_description = models.CharField(max_length = 16, null=True, blank=True)
    result_info = models.JSONField()

    def __str__(self):
        return str(self.uid)   ", friend_id:"   self.friend_id

    class Meta:
        constraints = [
        models.UniqueConstraint(fields=['uid', 'friend_id'], name='composite_key')
        ]

I thought that the problem may be in lack of superuser, so I tried to create one. However, I got another problem: User Registration with error: no such table: auth_user. I read the solution here and it states that I need to do python manage.py migrate. But it is impossible, as I said previously.

This is a strange behavior because I still have no sqlite file. And exactly the same problem exists WITH sqlite file. Please note that it is NOT duplicate of this question because I have different traceback, also I have much newer version of Django (4.1), as well as the site works normally on localhost.

CodePudding user response:

You likely have a setting:

AUTH_USER_MODEL = 'auth.User'

so with a capital U. This will refer to the user model.

You probably did not migrate the auth model. You can do this first with:

python manage.py migrate auth
  • Related