Django-rest-framework: DatabaseError: relation "authtoken_token" does not exist

Created on 6 Mar 2013  路  6Comments  路  Source: encode/django-rest-framework

Versions:

  • Django==1.5
  • djangorestframework==2.2.2

INSTALLED_APPS:
'rest_framework',

I get the following DatabaseError when trying to delete a user (i use a custom user model) in Django Admin:
DatabaseError: relation "authtoken_token" does not exist
LINE 1: ..."."is_active", "accounts_user"."date_joined" FROM "authtoken...

Sqlite does not raise an error. Using the postgresql_psycopg2 backend does.

A quick search got me to the following issue http://stackoverflow.com/questions/15160315/how-to-disable-importing-authtoken-models-when-using-sessionauthentication-with that describes a similar behaviour.

Adding rest_framework.authtoken to INSTALLED_APPS and migrating to authtoken:0001_initial solved the problem for me, although i never intended to use rest_framework.authtoken.

I dont know yet what exactly is causing the problem, but maybe this information helps to narrow down the issue.

Bug

Most helpful comment

@tobiasfuhlroth Considering this workaround...

from django.conf import settings

class Token(models.Model):
    ...

    class Meta:
        abstract = 'rest_framework.authtoken' not in settings.INSTALLED_APPS

Any chance you could give it a try?

All 6 comments

Could you throw over a full stacktrace?

There you go:

Traceback (most recent call last):

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, _callback_args, *_callback_kwargs)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/contrib/admin/options.py", line 372, in wrapper
return self.admin_site.admin_view(view)(_args, *_kwargs)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
response = view_func(request, _args, *_kwargs)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/views/decorators/cache.py", line 89, in _wrapped_view_func
response = view_func(request, _args, *_kwargs)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/contrib/admin/sites.py", line 202, in inner
return view(request, _args, *_kwargs)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/utils/decorators.py", line 25, in _wrapper
return bound_func(_args, *_kwargs)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
response = view_func(request, _args, *_kwargs)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/utils/decorators.py", line 21, in bound_func
return func(self, _args2, *_kwargs2)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/contrib/admin/options.py", line 1205, in changelist_view
response = self.response_action(request, queryset=cl.get_query_set(request))

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/contrib/admin/options.py", line 960, in response_action
response = func(self, request, queryset)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/contrib/admin/actions.py", line 35, in delete_selected
queryset, opts, request.user, modeladmin.admin_site, using)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/contrib/admin/util.py", line 109, in get_deleted_objects
collector.collect(objs)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/contrib/admin/util.py", line 160, in collect
return super(NestedObjects, self).collect(objs, source_attr=source_attr, **kwargs)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/db/models/deletion.py", line 225, in collect
elif sub_objs:

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/db/models/query.py", line 141, in nonzero
return type(self).bool(self)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/db/models/query.py", line 135, in bool
next(iter(self))

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/db/models/query.py", line 123, in _result_iter
self._fill_cache()

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/db/models/query.py", line 939, in _fill_cache
self._result_cache.append(next(self._iter))

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/db/models/query.py", line 344, in _safe_iterator
for item in iterator:

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/db/models/query.py", line 301, in iterator
for row in compiler.results_iter():

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 775, in results_iter
for rows in self.execute_sql(MULTI):

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 840, in execute_sql
cursor.execute(sql, params)

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 58, in execute
six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])

File "/home/notch/virtualenvs/example-project/lib/python2.6/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 54, in execute
return self.cursor.execute(query, args)

DatabaseError: relation "authtoken_token" does not exist
LINE 1: ..."."is_active", "accounts_user"."date_joined" FROM "authtoken...

I replaced the actual project name in the path with example-project. Everything else stays the same.

Issue is this: https://code.djangoproject.com/ticket/19422

We can do a workaround in the meantime.

Thanks for pointing that out.

Maybe you can work around this bug by making sure that the module containing the offending models isn't imported, ever. With Plata that wasn't too hard, I don't know about authtoken though.

Fixing the issue for real might be less work though...

@tobiasfuhlroth Considering this workaround...

from django.conf import settings

class Token(models.Model):
    ...

    class Meta:
        abstract = 'rest_framework.authtoken' not in settings.INSTALLED_APPS

Any chance you could give it a try?

Was this page helpful?
0 / 5 - 0 ratings