No such table error

Hi, I am new to Django, and have come across an issue while following the tutorial from the documentation.

My app is slightly different from the tutorial, but its very similar. In my models file I’ve created a class “Person”. When I go into the Python shell, I create a Person p1 with the constructor as defined in models. But when I write

p1.save()

I get this error:

“django.db.utils.OperationalError: no such table:”

I’ve deleted the migration file and made a new migration, but I keep getting this error, for any object I try to create and save. When I makemigrate, it acknowledges changes made to the models file, but then when I migrate, I get the response:

No migrations to apply.

Any help is very appreciated, thanks in advance!

Django tracks what migrations have been performed. When you delete a migration file and recreate it, Django doesn’t realize that it’s a new migration to be applied.

My general recommendation once someone has deleted a migration file is to save any needed data from the database, drop the database and recreate it, apply all migrations, then reload the saved data.

Yes, there are other ways this can be resolved, but they tend to require some messing around with things - and when you’re working with a tutorial, dropping and recreating the database is generally the easiest solution.

Ken

1 Like