Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 579
Aymane Shuichi Avatar asked Aug 30 '15 17:08

Aymane Shuichi


2 Answers

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.

like image 185
Rahul Gupta Avatar answered Nov 07 '22 07:11

Rahul Gupta


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.

like image 23
Amrendra Avatar answered Nov 07 '22 08:11

Amrendra