Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework not creating table authtoken_token

Im trying to set up Django Rest Framework authtoken, From what I understand the table authtoken_token should be created after makemigrations and migrate. I added rest_framework to settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Site',
    'rest_framework',
    'rest_framework.authtoken',
    'MySQLdb'
]

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAdminUser'
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ),
}

The migration commands output:

'manage.py@MySite > makemigrations Site
"C:\Program Files (x86)\JetBrains\PyCharm 2016.2\bin\runnerw.exe" C:\Python27\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 2016.2\helpers\pycharm\django_manage.py" makemigrations Site "C:/Users/Eric Franzen/PycharmProjects/MySite"
No changes detected in app 'Site'

Process finished with exit code 0
manage.py@MySite > migrate Site
"C:\Program Files (x86)\JetBrains\PyCharm 2016.2\bin\runnerw.exe" C:\Python27\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 2016.2\helpers\pycharm\django_manage.py" migrate Site "C:/Users/Eric Franzen/PycharmProjects/MySite"
Operations to perform:
  Apply all migrations: Site
Running migrations:
  No migrations to apply.

Process finished with exit code 0' If anyone has any idea why the table is not being created the information would be appreciated!

like image 343
enrique2334 Avatar asked Aug 16 '16 18:08

enrique2334


2 Answers

running migrate authtoken fixed this

like image 195
enrique2334 Avatar answered Nov 09 '22 17:11

enrique2334


I was using migrate authtoken due to a similar issue where the tables weren't being created.

But when running tests the migrations would fail, because of another migration on my app to store a Token on the superuser; manage.py test was not running the migrations in the order I needed.

The solution in my case was to add a dependency to my app's migration that needed Token, looking like:

dependencies = [
        ('api', '0005_auto_20200221_2108'), # the prior migration
        ('authtoken', '0002_auto_20160226_1747'), # the last authtoken migration I saw in showmigrations
    ]

After adding the dependency the migrations run successfully, including when I run test, and I can remove migrate authtoken from my workflow.

I'm not sure this solves the mystery of why the authtoken migrations weren't being run at all in the original scenario, but maybe it helps unstick someone.

like image 36
Kate Avatar answered Nov 09 '22 17:11

Kate