5

Here's what I'm doing all the time, and I guess it's not the right solution:

  • modify my models
  • delete the db.sqlite3 file
  • launch makemigrations then migrate

If I dont delete the db.sqlite3 file I have this when I try to run makemigrations then migrate:

manage.py@pyweb > makemigrations
"C:\Program Files (x86)\JetBrains\PyCharm 4.5.3\bin\runnerw.exe" C:\Python34\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 4.5.3\helpers\pycharm\django_manage.py" makemigrations C:/Users/Olivier/PycharmProjects/pyweb
No changes detected

Process finished with exit code 0
manage.py@pyweb > migrate
"C:\Program Files (x86)\JetBrains\PyCharm 4.5.3\bin\runnerw.exe" C:\Python34\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 4.5.3\helpers\pycharm\django_manage.py" migrate C:/Users/Olivier/PycharmProjects/pyweb
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages, produits
  Apply all migrations: sessions, auth, contenttypes, admin
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  No migrations to apply.

Process finished with exit code 0

What step am I missing to avoid deleting the db file?

Olivier Pons
  • 15,363
  • 26
  • 117
  • 213

1 Answers1

3

After changing your model you just do

python manage.py makemigrations your_app

And then migrate it with

python manage.py migrate

Doing this you dont have to delete your database. I hope this helpts you.. But dont forget run makemigrations after you apply changes to your model

gamer
  • 5,673
  • 13
  • 58
  • 91
  • When I do this, I get: `django.db.utils.OperationalError: table "xxx" already exists` – Olivier Pons Jul 28 '15 at 11:33
  • 1
    Then u have done something wrong with your migration already. Try commenting your model and generate the migration with above steps and after that again uncomment it and again generate migration and migrate it – gamer Jul 28 '15 at 11:36
  • If you can read migration file then just delete that migration file which creates the table that u just created. And run migration that will help you.. But be careful only delete migration that has created your XXX table – gamer Jul 28 '15 at 11:37
  • I've tried to delete the whole migration folder, then re-launch, and same problem. My main worry is when my website will be launched, if I have same problems, I wont be able to handle them properly. Anyway, I've re-created once again the database. I'll try your solution, if it work I'll check your answer as valid. Thank you very much for taking the time to answer. – Olivier Pons Jul 28 '15 at 11:47
  • I have also faced this problem in production. You just have to handle the migration if anything occurs you can delete your migration file which was creating the issue... – gamer Jul 28 '15 at 11:49
  • Following your steps seems to works, except when I'm moving some fields from one table to another: `django.db.utils.IntegrityError: UNIQUE constraint failed: description__new.slug`. So I've decided to be extremely careful with modifying existing columns, and I have to make sure I wont make any big changes, because I dont want to have this kind of problems... If I delete the migration folder, I get exception in the admin `no such column: description.slug`. – Olivier Pons Jul 28 '15 at 13:02
  • You dont have to delete whole migration folder just comment your changes and delete that migration which has the new changes. and uncomment the changes in model and create migration again and migrate.. you should know which migration file to delete.. there is detail explanation on migration file.. If your new field is not set to blank and null it will give error so try giving default value when adding new field to the model. – gamer Jul 29 '15 at 04:09
  • I check your answer as valid, even though, from my 12hours non-stop tests, you have to be very careful, and change as few as possible the database schema. Only adding new tables works like a charm. – Olivier Pons Jul 29 '15 at 11:41