2

In my django application, I used to authenticate users exploiting base django rest framework authentication token. Now I've switched to Json Web Token, but browsing my psql database, I've noticed the table authtoken_token, which was used to store the DRF authentication token, is still there. I'm wondering how to get rid of it. I've thought about 2 options:

  1. deleting it through migration: I think this is the correct and safer way to proceed, but in my migrations directory inside my project folder, I didn't find anything related to the tokens. Only stuff related to my models;
  2. deleting it directly from the database could be another option, but I'm afraid of messing with django migrations (although it shoudn't have links with other tables anymore)

I must clarify I've already removed rest_framework.authtoken from my INSTALLED_APPS

dc_Bita98
  • 851
  • 2
  • 17
  • 35

2 Answers2

2

To remove the authtoken tables you may use django's migration command in the following way:

python manage.py migrate authtoken zero

This will revert all the migrations from the authtoken app, effectively removing the tables from the database (as long as this doesn't break any constraints).

The zero migration can be used with any installed django app (see relevant django documentation).

After running the zero migration, you may remove the app from INSTALLED_APPS

Ire
  • 1,341
  • 10
  • 16
  • Can I delete the **authtoken** files after running your command? As suggested in points **#2** and **#3** of the previous answer? – dc_Bita98 May 15 '20 at 14:15
  • 1
    @dc_Bita98 yes, the command will delete the database tables associated with the authtoken app. You can then remove all the references from your code (INSTALLED_APPS, etc). I will update the answer to clarify : ) – Ire May 15 '20 at 14:25
  • I think this should be the right way of proceeding, but I've previously remove **rest_framework.authtoken** from my **INSTALLED_APPS**, so when I run that command, I get the following error `CommandError: No installed app with label 'authtoken'` – dc_Bita98 May 15 '20 at 14:26
  • 1
    Migrations can only be run for apps that are installed. The app can be removed from the INSTALLED_APPS setting after running the command. – Ire May 15 '20 at 14:30
1

You can choose the first option. There are 3 steps should you do to complete uninstall authtoken from your Django app

  1. Remove rest_framework.authtoken from INSTALLED_APPS, this action will tell your Django app to do not take any migrations file from that module
  2. Remove authtoken_token table, if you will
  3. Find the record with authtoken app name in table django_migrations, you can remove it.

Note: There are several error occurs in your code, because authtoken module is removed from your INSTALLED_APPS. My advice, backup your existing database first before you do above step

Eby Sofyan
  • 434
  • 4
  • 8
  • I advise anyone who read this question to also follow this answer, cause it provides other necessary steps to completely delete the app. Thank you – dc_Bita98 May 15 '20 at 14:40