1

When I try to delete a user from Django's built in auth app, I have this error: django.db.utils.OperationalError: no such table: main.auth_user__old

I searched on the web and I found this question : Django - No such table: main.auth_user__old So I updated Django and I am now using the version 3.2.4 (I am using sqlite). Then I tried to delete auth tables from the db but when I did a ptyhon manage.py makemigrations auth, it told me that no changes were detected. Also, I can't delete the whole database.

Does someone have an idea what I can do to fix this?

Arthur
  • 27
  • 4
  • Did you run `python manage.py migrate`? – Pedram Parsian Jun 17 '21 at 07:15
  • Try `python manage.py migrate auth zero --fake` and then `python manage.py migrate`. You really shouldn't go and delete the tables yourself, the migration system can do that easily. – Abdul Aziz Barkat Jun 17 '21 at 07:16
  • Thank you @AbdulAzizBarkat, it worked ! However, the tables were not deleted automatically with your command, I had to do it again by hand. Does it have something to do with the '--fake' tag ? – Arthur Jun 17 '21 at 07:47
  • I thought you had _already_ deleted the tables? That is why I included `--fake` in the command. – Abdul Aziz Barkat Jun 17 '21 at 07:48
  • Yes I had already deleted them, but because nothing worked, I restored an old version of the database to try something else. – Arthur Jun 17 '21 at 08:08

1 Answers1

1

You shouldn't be deleting tables manually while using Django. Django adds an extra table to the database called django_migrations and in this table Django keeps track of which migrations are applied. When you manually delete stuff their entry is still left in this table and hence you find yourself unable to apply migrations.

The simplest way to do what you want is to reverse the migrations by migrating to a previous migration by specifying the migration name:

python manage.py [app_label] [migration_name]

You can use zero as the migration_name to reverse all migrations. If suppose you have already changed things manually you can use the --fake flag to fake the migrations (Which will simply mark / unmark the migrations as applied). Considering you already deleted the tables you can use the following command:

python manage.py migrate auth zero --fake

For more information on the migrate command you can read the documentation.

Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33