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?

Dan
  • 571
  • 1
  • 7
  • 13

4 Answers4

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

Mohammed Shareef C
  • 3,829
  • 25
  • 35
  • 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 '17 at 05:38
  • Anyone understand why I am still getting errors, "no such table: table_name"? presumably i've deleted everything.....? – Nathaniel Hoyt Apr 20 '22 at 02: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 – Mohammed Shareef C Apr 22 '22 at 09: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.

Zaeem
  • 139
  • 2
  • 4
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

iklinac
  • 14,944
  • 4
  • 28
  • 30
  • 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 '17 at 02: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 '17 at 02:49
  • If you delete migration files, remember not to delete __init__ file – Mohammed Shareef C Feb 10 '17 at 06: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

trex
  • 3,848
  • 4
  • 31
  • 54