如何为新服务器运行迁移

Posted

技术标签:

【中文标题】如何为新服务器运行迁移【英文标题】:How to run migrations for a new server 【发布时间】:2015-09-08 15:36:31 【问题描述】:

我正在尝试在 Heroku 上部署我的 Django-cms 站点,但我真的在为迁移而苦苦挣扎。我正在运行 django 1.7.7 和 django-cms 3.1.0。我没有要迁移的数据,所以我只想创建空表。

使用全新的 Heroku Postgres 数据库:

heroku run python manage.py migrate contenttypes

输出:

Operations to perform:
Apply all migrations: contenttypes
Running migrations:
  Applying contenttypes.0001_initial...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/app/.heroku/python/lib/python3.4/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/app/.heroku/python/lib/python3.4/site-packages/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/app/.heroku/python/lib/python3.4/site-packages/django/db/migrations/executor.py", line 96, in apply_migration
    if self.detect_soft_applied(migration):
  File "/app/.heroku/python/lib/python3.4/site-packages/django/db/migrations/executor.py", line 140, in detect_soft_applied
    apps = project_state.render()
  File "/app/.heroku/python/lib/python3.4/site-packages/django/db/migrations/state.py", line 75, in render
    "for more" % new_unrendered_models
django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [<ModelState: 'djangocms_link.Link'>, <ModelState: 'djangocms_text_ckeditor.Text'>, <ModelState: 'djangocms_file.File'>, <ModelState: 'djangocms_inherit.InheritPagePlaceholder'>, <ModelState: 'djangocms_column.MultiColumns'>, <ModelState: 'djangocms_column.Column'>, <ModelState: 'djangocms_googlemap.GoogleMap'>, <ModelState: 'djangocms_flash.Flash'>, <ModelState: 'djangocms_snippet.SnippetPtr'>, <ModelState: 'djangocms_video.Video'>, <ModelState: 'djangocms_teaser.Teaser'>, <ModelState: 'djangocms_picture.Picture'>]
This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)
 in an app with no migrations; see https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies for more

我不明白的是 contenttypes 不依赖于 djangocms 插件中的任何模型。如果我尝试先迁移任何插件,我会得到一个堆栈跟踪,并且:

RuntimeError:创建新内容类型时出错。请确保在尝试单独迁移应用之前迁移内容类型。

更新:根据各种 django-cms 插件的文档,我将 MIGRATION_MODULES 添加到 settings.py。现在当我跑步时:

heroku run python manage.py makemigrations djangocms_text_ckeditor

创建迁移:

Migrations for 'djangocms_text_ckeditor':
  0001_initial.py:
    - Create model Text

听起来不错!但是……

heroku run python manage.py migrate djangocms_text_ckeditor

输出:

Operations to perform:
  Apply all migrations: (none)
Running migrations:
  No migrations to apply.
  Your models have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.4/site-packages/django/contrib/contenttypes/models.py", line 44, in get_for_model
    ct = self._get_from_cache(opts)
  File "/app/.heroku/python/lib/python3.4/site-packages/django/contrib/contenttypes/models.py", line 34, in _get_from_cache
    return self.__class__._cache[self.db][key]
KeyError: 'default'

During handling of the above exception, another exception occurred:
[long stack trace ommitted]

【问题讨论】:

我是否需要使用MIGRATION_MODULES 设置才能将应用迁移包含在源代码管理中并推送到heroku? 【参考方案1】:

djangocms_link 模块文档中所述 - https://github.com/divio/djangocms-link

如果使用 Django 1.7 添加 'djangocms_link': 'djangocms_link.migrations_django', 到 MIGRATION_MODULES

djangocms_file 模块也一样

所以基本上你应该在你的settings.py中有

MIGRATION_MODULES = 
     'djangocms_link': 'djangocms_link.migrations_django',
     'djangocms_file': 'djangocms_file.migrations_django'

【讨论】:

好的,我将 MIGRATION_MODULES 放回 settings.py 中(两种方法都试过了),我会更新我的问题。 您是否尝试使用 heroku run python manage.py migrate 一次运行所有迁移?

以上是关于如何为新服务器运行迁移的主要内容,如果未能解决你的问题,请参考以下文章

代码优先迁移:如何为新属性设置默认值?

如何为实体框架代码优先迁移设置隔离级别

如何为 laravel 迁移指定文件夹?

如何为 postgresql 数组数据类型创建 laravel 迁移

如何为 PHP/MySQL 应用程序自动迁移(模式和数据)

如何为多个数据库启用迁移,但只有一个上下文?