简体   繁体   中英

Django 1.8 Syncdb vs migrate

I have created a model and executed syncdb which had created the tables as my model was designed.
Afterwards I modified the model and executed makemigrations which created the migrations ignoring the tables that syncdb had already created.

So I ended up with an error "relation already exists".

Why did makemigrations created everything from scratch? How do I fix this situation ?

makemigrations creates new migrations based on the changes detected to your models.

Also, one thing to note is syncdb command is deprecated since Django 1.7 and will be removed in Django 1.9. So, you should use the migrate command.

From syncdb docs:

Deprecated since version 1.7:
This command has been deprecated in favor of the migrate command , which performs both the old behavior as well as executing migrations.

makemigration always creates one migration file having all the changes. So, when you run makemigration for first time it tries to find the previous migration file. if not found it creates one initial migration file. And when it tries to apply it to the db it finds the relation already exists. And thus throws error. Best practice is, before updating model, create one migration then modify the model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM