【问题标题】:How to apply django initial migration?如何应用 django 初始迁移?
【发布时间】:2019-01-14 11:21:13
【问题描述】:

showmigrations 显示有 31 个可用迁移:

# python3 manage.py showmigrations
admin
 [X] 0001_initial
 [X] 0002_auto_20190114_1409
 [X] 0003_auto_20190114_1410
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_auto_20190114_1409
authtoken
 [X] 0001_initial
 [X] 0002_auto_20160226_1747
 [X] 0003_auto_20190114_1409
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
 [X] 0003_auto_20190114_1409
exchange_delivery
 [X] 0001_initial
 [X] 0002_enlarge_phone_field
 [X] 0003_unique_external_id
 [X] 0004_add_warehouse
 [X] 0005_add_delivery_point_type
 [X] 0006_update_delivery_type_operating_mode
 [X] 0007_add_delivery_point_region_model
 [X] 0008_update_warehouse_exchange
 [X] 0009_add_verbose_name_for_warehouse_and_add_delivery_point_banned_group
 [X] 0010_add_active_flag_to_warehouse
 [X] 0011_auto_20190114_1409
sessions
 [X] 0001_initial
 [X] 0002_auto_20190114_1410
volt
 [X] 0001_initial
 [X] 0002_auto_20190114_1410

如何应用所有这些迁移? migrate 表示No migrations to apply

# python3 manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: corsheaders, export, opinion, volt.integration1c.delivery, custom_logger, event_listener, legacy, region, order, catalog, promo_table, messages, staticfiles, api, best_product, shop, general, market_cpa, rest_framework, delivery, exchange, talk
  Apply all migrations: auth, authtoken, sessions, exchange_delivery, admin, volt, contenttypes
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
/usr/lib/python3.6/site-packages/django/core/management/commands/loaddata.py:239: RemovedInDjango19Warning: initial_data fixtures are deprecated. Use data migrations instead.
  RemovedInDjango19Warning

Installed 0 object(s) (of 6) from 1 fixture(s)
Running migrations:
  No migrations to apply.

我不熟悉 Django,我只需要 dockerize 现有的应用程序。

据我所知,python3 manage.py syncdb 命令的输出

django.db.utils.ProgrammingError: (1146, "Table '220-django.auth_user' doesn't exist")

问题是没有应用 django 和 django-admin 的初始迁移

项目在Django 1.8.14

【问题讨论】:

  • 尝试将python3 manage.py migrate app_name 用于基本应用程序。将 app_name 替换为您的基本应用。
  • 试试this answer
  • @Sam,如何定义基础应用程序?我在INSTALLED_APPS general/settings.py 有 5 个顶级应用程序

标签: python django


【解决方案1】:

如我所见,所有迁移的名称旁边都有 [X]。表示这些迁移已应用,as per documentation。如果有任何未应用的迁移,那么它将在迁移名称旁边显示为 [ ]。更多详情(是否已应用迁移),请查看 DB 中的django_migrations 表。

【讨论】:

  • 是否有可能 [X] 是虚假迁移的结果,而 showmigrations 迁移实际上并未应用?我在问题中添加了 python3 manage.py syncdb 的奇怪输出
  • 是的,如果您伪造应用迁移,它将显示[X],因为在django_migrations 表中已创建条目。
  • 我不知道你为什么要使用syncdb。它被贬低了,只是复制了python manage.py migrate。我认为你可以查看这个关于 dockerizing django 的 repo:github.com/ruddra/docker-django
  • 感谢您提供回购链接!我在生产中检查了数据库 220-django,它有以下表格 paste.ubuntu.com/p/zDSfjPC44G 你能告诉我,它是标准的 Django 表格吗?
  • 是的。它们是具有默认应用程序的 django 项目的标准表。
【解决方案2】:

您的数据库中的django_migrations 表中有一些条目,因此迁移显示No migrations to apply。 要解决这个问题,请转到数据库控制台并运行以下命令

delete from django_migrations;

或者,直接去数据库表中删除所有行。

然后运行

python manage.py migrate

【讨论】:

    猜你喜欢
    • 2014-10-25
    • 2015-06-27
    • 2017-09-05
    • 1970-01-01
    • 2015-09-06
    • 2011-11-19
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    相关资源
    最近更新 更多