18

I am trying to refactor a Django project. I renamed a couple apps and added a new one, as well as shuffled some models around. I want to clear my database and migrations and start fresh, but I am not sure how to accomplish this. Here's what I did:

rm -r myapp/migrations // I ran this for all my apps    
python manage.py flush
python manage.py makemigrations myapp // I ran this for all my apps
python manage.py migrate // This errors

I get an error:

django.db.utils.OperationalError: table "myapp_mymodel" already exists

Can anyone tell me what I might be doing wrong?

EDIT: What is the django command to delete all tables? did not work.

Community
  • 1
  • 1
InnisBrendan
  • 2,079
  • 2
  • 19
  • 21
  • Just delete the `.sqlite` file as well. It will destroy your data, but you don't seem to be bothered by that. – code_dredd May 07 '16 at 01:13
  • Possible duplicate of [What is the django command to delete all tables?](http://stackoverflow.com/questions/10605940/what-is-the-django-command-to-delete-all-tables) – trinchet May 07 '16 at 01:29
  • `flush` just drop the data, not the structure of your DB. You can see more here http://stackoverflow.com/questions/10605940/what-is-the-django-command-to-delete-all-tables – trinchet May 07 '16 at 01:29
  • You have to delete the migration files manually. – winux Dec 21 '18 at 04:10

5 Answers5

45

Delete database and delete migration files (.py and .pyc) in migrations directory of your app (don't delete __init__.py file). Then run python manage.py makemigrations app and python manage.py migrate.

vsd
  • 1,473
  • 13
  • 11
  • which one should run first? 'python manage.py makemigrations app' or 'python manage.py migrate' ? – KKlalala Feb 16 '17 at 02:45
  • 1
    `python manage.py makemigrations app` obviously - you have to have something to migrate – Sebastian Suchanowski Feb 26 '17 at 17:26
  • 1
    Its correct to delete migration files and then run flush but deleting sqlite database file is wrong. This worked to me every time. If you are using other database, it will save you a lot of work and preparations. 1) delete all ".py" and ".pyc" files 2) >python manage.py flush type "yes" to confirm 3) >python manage.py makemigrations 4) >python manage.py migrate – winux Dec 21 '18 at 03:57
5

I had the same issue, using Django 1.10, here is what I did, I deleted the database sqlite file, deleted the pycache folders inside each of the apps, deleted all files inside the migrations folder for each app , except the init.py file, and then ran python manage.py makemigrations and python manage.py migrate. Also note that because you deleted the database you will have to create a new superuser using python manage.py createsuperuser. Hope this helps

Daniel Vieira
  • 461
  • 5
  • 19
3

For me, just

python manage.py flush

deleted old db contents, so i was able to create records anew in Django 2.1.4.

Don't forget to create new superuser:

python manage.py createsuperuser
WebComer
  • 1,131
  • 2
  • 19
  • 31
1

This may help you if you want to clear sqlite3 DB follow these steps.

  1. Delete migrations files except init.py
  2. Delete dbsqlit3 file
  3. Then type python/python3 manage.py migrate
  4. Then make changes in your models
  5. Type python/python3 manage.py makemigrations
  6. Type python/python3 manage.py migrate
  7. Then you have to create new superuser by just typing python/python3 manage.py createsuperuser . you should use new name not old user name
Israr Awan
  • 55
  • 7
0

Do not delete your database file!

Its correct to delete migration files and then run flush but deleting sqlite database file is wrong. This worked to me every time. If you are using other database, it will save you a lot of work and preparations.

  1. delete all ".py" and ".pyc" files manually
  2. python manage.py flush
    type "yes" to confirm
  3. python manage.py makemigrations
  4. python manage.py migrate
winux
  • 452
  • 4
  • 12