What should I use instead of syncdb in Django 1.9?
Asked Answered
B

4

74

Take a look at this:

$ pypy ./manage.py syncdb
/usr/lib64/pypy-2.4.0/site-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
  warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)

(cut)

I ran a quick google search, but could not find the answer - what should I be using instead of syncdb?

Brake answered 16/4, 2015 at 18:49 Comment(1)
Actually, now that I that I look again, it's right there: syncdb has been deprecated and replaced by migrateBrake
C
93

syncdb is deprecated because of the migration system, introduced with django 1.7.

Now you can track your changes using makemigrations. This transforms your model changes into python code to make them deployable to another databases. When you have further modifications you need applied to the database, you can use data migrations.

After you created the migrations you have to apply them: migrate.

So instead of using syncdb you should use makemigrations and then migrate.

Workflow on development after you changed something in your models:

./manage.py makemigrations
./manage.py migrate

And on your production system:

./manage.py migrate

Bonus: you do not need to run migrate for each change. If you have multiple changes not applied yet django will run them in the correct order for you.

Choriamb answered 16/4, 2015 at 19:3 Comment(3)
It is the best new feature ;)Venice
NOTE: makemigrations may require the module name as a parameter.Brake
This is terrible advice. If you're installing an app with a dozen migrations to a blank database, you should never run migrate because that will be a huge waste of time. @Don Mums answer is the correct answer.Watteau
L
64

You should definitely use migration system. Which lets you track changes in your models.py, and create migrations for the database. The migration system uses the commands makemigrations to create migrations and migrate to migrate the database.

If for whatever reason you need to create a database the same way syncdb did it there is command flag that causes migrate to work the same way. You should only do this if you REALLY need it and you know what you are doing. For example to create an empty database on for a continuous integration system of your choice.

python manage.py migrate auth
# performs migrations for auth and contenttypes contrib apps

python manage.py migrate --run-syncdb
# creates the rest of the database

Tested on Django 1.9.1.

Lanyard answered 6/1, 2016 at 14:48 Comment(2)
+1 for the actual solution, this should be the answer. While you should use migrations for production releases when actively developing from scratch it is easier to just wipe out the DB and start over than need to run 500 migrations. I routinely run dropdb mydb && createdb mydb && python manage.py migrate --run-syncdb whenever I make a change.Protrusile
--run-syncdb only seems to work on apps that don't have migrations. I used the following workaround: find -name "migrations" -exec mv {}/__init__.py {}/__init__ \; && python manage.py migrate && python manage.py migrate --run-syncdb && find -name "migrations" -exec mv {}/__init__ {}/__init__.py \; && python manage.py migrate --fakePrenatal
N
9

You should use the makemigrations and migrate commands that were introduced in django 1.7

https://docs.djangoproject.com/en/1.7/topics/migrations/

Nimiety answered 16/4, 2015 at 18:50 Comment(0)
W
3

syncdb has some problem with db migration. so, after django 1.7 makemigrations and migrate have been introduced. Now in django 1.9 syncdb has been deprecated. try
1. python manage.py makemigrations which detects changes in db and creates one .py file as inside migrations folder 2. python manage.py migrate will apply the migrations to the database

Whippersnapper answered 29/6, 2016 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.