41

I made a spelling error in my model and now one of my columns is misspelled. I'd like to drop all tables in the database, fix the error in the model.py, and recreate the database with the correct spelling in model.

I've tried to use the suggestions in the this article but the table still exists after I follow the commands outlined there.

Anyone have a quick way to do this?

4 Answers 4

97
  1. Delete the sqlite database file (often db.sqlite3) in your django project folder (or wherever you placed it)
  2. Delete everything except __init__.py file from migration folder in all django apps (eg: rm */migrations/0*.py)
  3. Make changes in your models (models.py).
  4. Run the command python manage.py makemigrations or python3 manage.py makemigrations
  5. Then run the command python manage.py migrate.

That's all.

If your changes to the models are not detected by makemigrations command, please check this answer

3
  • Due to the auto complete feature in Atom I found that I had the typo in a few different places. Once I mixed them all this suggestion worked. Thank you!
    – Dan
    Feb 10, 2017 at 5:38
  • Anyone understand why I am still getting errors, "no such table: table_name"? presumably i've deleted everything.....? Apr 20, 2022 at 2:31
  • 1
    @NathanielHoyt This normally happens when there are one or more migrations to be applied. Pls ensure you have executed python manage.py migrate. Another case is that some migrations are not applied to your existing database but are marked as applied. One thing you can do is to find the app to which this table belongs and find the migration file which contains code like migrations.CreateModel( name='my_table_name', ...... ), Comment out this part of code and run makemigrations again. then run migrate again Apr 22, 2022 at 9:03
13

rm -f tmp.db db.sqlite3 rm -r my-app/migrations python manage.py makemigrations python manage.py migrate

Removes the database.
Removes the migrations from your app.
Re-runs the migrations. Note: you could also do: python manage.py makemigrations my-app
Migrate changes.

1
  • 1
    Thank you! This is the answer that the layman actually needs!
    – deps_stats
    Oct 14, 2021 at 2:05
0

You can just delete your sqlite file.

Regarding your question, you should use Django migration system to do database changes for you project using makemigrations and migrate commands

migration docs

3
  • I moved the db.sqlite3 file to the trash. Then when I try to makemigrations I get this error: "django.core.exceptions.FieldError: Unknown field(s) (company_industy) specified for CompanyProfile" company_industy was the mispelling I want to fix.
    – Dan
    Feb 10, 2017 at 2:46
  • considering you are doing everything from scratch you could delete previous migrations, otherwise you should debug through your migration files and see where the problem is
    – iklinac
    Feb 10, 2017 at 2:49
  • If you delete migration files, remember not to delete init file Feb 10, 2017 at 6:00
0

To do everything in one go, run all below commands in a shell script (Linux flavors) or in a batch file (Windows). and run that script where your manage.py exists:

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete
find . -path "*.sqlite3"  -delete
python manage.py makemigrations
python manage.py migrate
python manage.py migrate
python manage.py shell -c "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', 'adminpass')"

I prefer them running by copy-pasting directly in one go. make sure to change superuser credentials.

Note- This will DELETE all databases and their corresponding migrations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.