skip to Main Content

I added this line in my settings.py file in order to make a custom User model the auth_user database: AUTH_USER_MODEL = 'main.User' (main is the name of one my apps and it contains all of the models).

But I later found out that I should have done this before the initial migration so when I tried to integrate Google Authentication in the admin page I received this error that stated that my auth_user table was empty (something about it not having any records with an id of 1).

So I tried deleting the migrations and returning the database to its state before the migrations but I always had an error like this: ValueError: The field socialaccount.SocialAccount.user was declared with a lazy reference to 'main.user', but app 'main' isn't installed.

I tried fixing this by following the adive on this article and cleared my postgresql database along with migration files and pycache. After this I tried to makemigrations and received this error: ValueError: The field socialaccount.SocialAccount.user was declared with a lazy reference to 'auth.user', but app 'auth' isn't installed.. And then this error when I tried to migrate: django.db.utils.ProgrammingError: relation "main_user" does not exist.

I have tried searching for solutions but I cannot find anything. What do I do?

settings.py (not all of it)

INSTALLED_APPS = [
    'landing',
    'main',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites', 
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
]

TEMPLATES = [
    {
        'DIRS': [
            os.path.join(BASE_DIR, 'landing', 'frontend', 'build'),
            os.path.join(BASE_DIR, 'landing', 'templates'),
            os.path.join(BASE_DIR, 'main', 'frontend', 'dist'),
        ],
        # ...
    },
]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        # ...
    }
}

AUTH_USER_MODEL = 'main.User'

STATICFILES_DIRS = [
    # ...
]

AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend'
]

SITE_ID = 1
LOGIN_REDIRECT_URL = '/'

# Additional configuration settings
SOCIALACCOUNT_QUERY_EMAIL = True
ACCOUNT_LOGOUT_ON_GET= True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_EMAIL_REQUIRED = True

SOCIALACCOUNT_PROVIDERS = {
    'google': {
        'SCOPE': [
            'profile',
            'email',
        ],
        'AUTH_PARAMS': {
            'access_type': 'online',
        }
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    I found a solution on this answer.

    I had already undone all migrations, deleted all /migration files and cleared all the data in my database. So to solve the main_user and main.User bugs I just had to makemigrations seperately with the main app by running:

    python manage.py makemigrations main
    

    And then migrate all the changes:

    python manage.py migrate
    

    This removed all the bugs I received previously and allowed me to connect Google Authentication with no further issues.


  2. You appear to have run into some difficulties while attempting to modify the AUTH_USER_MODEL setting in your Django project. After the first migration, changing the AUTH_USER_MODEL can be a difficult operation, and you must take specific precautions to protect data integrity. Here are some procedures you can follow to fix the issues you ran into:

    1. Build a backup: To prevent data loss, you must build a backup of your database before making any additional changes.

    2. Return to the initial state: Try returning to the initial state before to making the changes if you deleted the migration files and cleared the database. Make sure you have the original migration files and the database’s previous state.

    3. Before the first migration, set AUTH_USER_MODEL: You must make the adjustment to the AUTH_USER_MODEL prior to doing the initial migration for it to be effective. Follow these steps to accomplish this:

      a. With the exception of the initial migration, delete all migration files from all apps.

      b. Deleting the database: Deleting the database and creating a new one.

      c. Update AUTH_USER_MODEL: Before beginning the initial migration, set
      AUTH_USER_MODEL in your settings.py file to’main.User’.

      d. Create initial migrations and apply them to the database by running "python
      manage.py makemigrations" and "python manage.py migrate."

    4. Correct the socialaccount app reference: The error message "app "main" isn’t installed" suggests that the socialaccount app may have a reference to the "main" app. Make sure the socialaccount app does not depend on or include any references to the ‘main’ app in order to fix this. To find and correct any such references, you might need to evaluate your code and templates.

    5. Fix ‘auth’ app reference: The issue ‘app ‘auth’ isn’t installed’ suggests that there might be a reference to the ‘auth’ app in your project with the previous user model. Examine the code and templates for your project to look for and change any references to the previous user model.

    6. After completing the aforementioned procedures, carefully examine the migration process for any errors. Make sure the migration goes off without a hitch.

    It can be difficult to change the AUTH_USER_MODEL after the initial migration, therefore it’s important to carefully follow the instructions to prevent issues with data integrity. If you run into any problems, consult the Django documentation and ask for advice on the forums or in the Django community. Before making any big changes to your database, always make a backup of your data.

    Hope this helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search