简体   繁体   English

在 Django 1.9 中我应该使用什么来代替 syncdb?

[英]What should I use instead of syncdb in Django 1.9?

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 ?我在谷歌上进行了快速搜索,但找不到答案 - 我应该使用什么来代替syncdb

syncdb is deprecated because of the migration system , introduced with django 1.7 .由于django 1.7引入的迁移系统syncdb已被弃用。

Now you can track your changes using makemigrations .现在您可以使用makemigrations跟踪您的更改。 This transforms your model changes into python code to make them deployable to another databases.这会将您的模型更改转换为 Python 代码,使其可部署到其他数据库。 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 .创建迁移后,您必须应用它们: migrate

So instead of using syncdb you should use makemigrations and then migrate .因此,您应该使用makemigrations然后migrate而不是使用syncdb

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.奖励:您不需要为每个更改运行migrate If you have multiple changes not applied yet django will run them in the correct order for you.如果您有多个更改尚未应用,django 将为您按正确的顺序运行它们。

You should definitely use migration system .您绝对应该使用迁移系统 Which lets you track changes in your models.py , and create migrations for the database.这使您可以跟踪models.py更改,并为数据库创建迁移。 The migration system uses the commands makemigrations to create migrations and migrate to migrate the database.迁移系统使用命令makemigrations创建迁移和migrate迁移数据库。

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.如果出于某种原因您需要以与syncdb相同的方式创建数据库,则有命令标志会导致migrate以相同的方式工作。 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.在 Django 1.9.1 上测试。

You should use the makemigrations and migrate commands that were introduced in django 1.7您应该使用 django 1.7 中引入的makemigrationsmigrate命令

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

syncdb has some problem with db migration. syncdb在数据库迁移方面存在一些问题。 so, after django 1.7 makemigrations and migrate have been introduced.因此,在引入 django 1.7 makemigrationsmigrate之后。 Now in django 1.9 syncdb has been deprecated.现在在 django 1.9 syncdb已被弃用。 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 1. python manage.py makemigrations检测数据库中的变化并创建一个.py文件作为迁移文件夹 2. python manage.py migratepython manage.py migrate应用到数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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