Renan Fonteles
Renan Fonteles

Reputation: 61

no such table: social_auth_usersocialauth

I'm trying to develop an Django Application which uses Google API for authentication. Everything seems to work fine, however after login I got this error: no such table: social_auth_usersocialauth and I have no idea how to solve it.

enter image description here

Is there anyone who already had this same problem that could help me?

I appreciate any help, thanks.

Upvotes: 5

Views: 3690

Answers (5)

Matthew Kaye
Matthew Kaye

Reputation: 69

For me the issue was that I was missing social_django in INSTALLED_APPS in settings.py.

Ref. link to official docs.

Upvotes: 2

Yevgeny Streltsov
Yevgeny Streltsov

Reputation: 56

Same problem with Django 3.0.3 when integrating with Auth0, started app from scratch.

Got it to work using the answer above; since I was developing this code a while now, did not want to start from scratch. For "New Code," have to say that simply downgrading to older version and doing "python3 manage.py migrate" won't always do. Django 3.0.3 models.ForeignKey(...) works differently and old version of the social app won't work.

Got this work with Django 3.0.3 and social-auth-app-django 3.1. The goal is to generate all the tables using Django manage.py script and models in the install packages.

Problem encountered:

  1. settings.py needs to be modified for old app to generate tables using python migration tool.

  2. in the old app, model is incompatible with new version of django, this is what I got: TypeError: __init__() missing 1 required positional argument: 'on_delete'

It is obvious that latest version of social app lib does not generate the tables it requires.


Solution:

Basically the steps Renan Fonteles described in his post here are sound, his post helped. Temporarily downgrading to python-social-auth==0.2.21 works. To solve the problem you can take these steps:

  1. Remove all the site packages from the venv, and make a new venv as in: $ python3 -m venv .

  2. install all the requirements as before, but instead of latest version of social-auth-app-django run: $ pip install python-social-auth==0.2.21 so remove the social-auth-app-django from requirements.txt

  3. at this point, for django 3.0.3, manually edit the model for the app in .../site-packages, models.ForeignKey has to have an additional argument.

${DJANGO_ENV_DIR}/lib/python3.7/site-packages$ find . -name "*.py" -exec grep -Hn 'models.Foreign' {} \;
./social/apps/django_app/default/migrations/0002_add_related_name.py:25:            field=models.ForeignKey(related_name='social_auth', to=USER_MODEL)
./social/apps/django_app/default/migrations/0001_initial.py:97:                ('user', models.ForeignKey(
./social/apps/django_app/default/models.py:33:    user = models.ForeignKey(USER_MODEL, related_name='social_auth')

add , on_delete=models.CASCADE as the last argument to models.ForeignKey call in each file that came up above. Because, models.ForeignKey has to have additional argument: ... = models.ForeignKey(..., on_delete=models.CASCADE)

  1. manage.py won't install the 'social_django' app since the package downgrading to in this step does not have this app, the olden app is: 'social.apps.django_app.default'.

  2. Change the 'social_django' to the 'social.apps.django_app.default' in settings.py INSTALLED_APPS = [...] section, or add the former to the section.

  3. settings.py DATABASES ... can have a section like this:

    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3_user_authentication'),
    },
  1. Run: python manage.py migrate, provided settings.py has databases for your authentication app set up as above it should make you the tables you need.

  2. Upgrade to latest version of social-auth-app-django
    $ pip uninstall python-social-auth==0.2.21
    $ pip install social-auth-app-django
    now you can put the social-auth-app-django back into requirements.txt

Note: You can't really call python-social-auth==0.2.21 a dependency, to generate tables you need to add it to INSTALLED_APPS, at the end you have to remove it from INSTALLED_APPS.

  1. Change the app back from 'social.apps.django_app.default' to 'social_django'

  2. Now run python manage.py migrate again.

These steps make sqlite db file with the table you later need for the app to work, you might want to save / backup this dbfile if you ever do migrations on this database. The 'social_django' app will work now if you have all the rest of the settings.py correct.

  • If you are going to be using Postgres / MySQL / ... etc, you should setup up the database with the different setup than in step 5. make a separate schema specifically for this, you can use a tool like SQuirreL SQL Client http://squirrel-sql.sourceforge.net/ to export the DDL created.

These posts helped a lot to get the app to work:

Eventually, I got the authentication to work with Auth0 and Google. Hopping for a fix to social-auth-app-django to make this answer obsolete. The latest package should generate the schema if app is being developed from scratch, there should be no need to downgrade the social app export tables from it.

Upvotes: 0

Paolo Stefan
Paolo Stefan

Reputation: 10273

The problem is, python-social-auth is deprecated as of December 2016.

Migrating existing projects

According to the migration instructions here:

  1. pip install python-social-auth==0.2.21
  2. Run migrations: python manage.py migrate
  3. Install the required module for Django: pip install social-auth-app-django
  4. Run migrations again: python manage.py migrate
  5. Update Django's app list and the backend-related settings according to the settings paragraph of the same page.
  6. Now you can safely uninstall the old package: pip uninstall python-social-auth

New projects

  1. pip install python-social-auth==0.2.21
  2. pip install social-auth-app-django
  3. Set Django's app list and the backend-related settings according to the settings paragraph of the migration page.
  4. Apply migrations: python manage.py migrate
  5. Uninstall the old package: pip uninstall python-social-auth

Upvotes: 3

Renan Fonteles
Renan Fonteles

Reputation: 61

I solved the problem. It seems that python-social-auth has some problems with migrations.

  1. I had to uninstall the last version of python-social-auth
  2. I installed the version python-social-auth 0.2.21
  3. Applied migration (python manage.py migrate)
  4. Uninstalled python-social-auth 0.2.21
  5. Installed the last version of python-social-auth

At least in my case, the last version of python-social-auth could not properly migrate the database.

Upvotes: 1

ChidG
ChidG

Reputation: 3223

It looks like you've installed one of the social auth apps but haven't migrated your database since then.

Try: python manage.py migrate

Upvotes: 0

Related Questions