Django dev和测试postgres数据库之间的迁移差异

时间:2017-03-01 18:59:01

标签: django database postgresql

我有一个我正在开发的开发数据库,​​我遇到了添加外键字段的迁移问题。清除迁移文件夹并重做后,我最终不得不吹掉数据库。所以现在我有一个迁移文件...

问题是,我将代码提取到我的测试服务器,现在该数据库非常不同步(它不是,django认为它是。它实际上只需要添加一个表和一个字段)。虽然运行使迁移中断,因为我推送的迁移文件夹除了一次迁移外都没有,并且没有与测试服务器上的迁移文件夹一起使用。

关于如何协调这一点的任何想法,这是我的测试数据,因此在这里吹走数据库并开始新的问题不是一个问题,但当我推向生产时,这将是一个巨大的问题(和不能吹走那个数据库)。也许使用pg_dump转储数据/数据库,吹掉数据库,运行迁移并通过转储文件加载数据?

编辑:

我确实尝试手动创建自己的迁移,因为我处于makemigrations没有说新内容的状态。我的迁移文件:

它给我的错误(不知道这是告诉我的)

Operations to perform:
  Apply all migrations: filer, sessions, admin, auth, contenttypes, swsite, registration, easy_thumbnails
Running migrations:
  Rendering model states... DONE
  Applying swsite.0001_initial... OK
  Applying swsite.0002_auto_20170302_0841...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/lib64/python2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/usr/lib64/python2.7/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/lib64/python2.7/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/lib64/python2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/lib64/python2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
    operation.state_forwards(self.app_label, project_state)
  File "/usr/lib64/python2.7/site-packages/django/db/migrations/operations/fields.py", line 50, in state_forwards
    state.models[app_label, self.model_name_lower].fields.append((self.name, field))
KeyError: (u'swsite', u'cesiumentity')

迁移:

# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2017-03-02 15:41
from __future__ import unicode_literals

from django.db import migrations, models


import django.contrib.gis.db.models.fields
import django.db.models.deletion

class Migration(migrations.Migration):

    dependencies = [
        ('swsite', '0001_initial'),
    ]

    operations = [
      migrations.CreateModel(
            name='ZoneEntity',
            fields=[
                ('zone_number', models.CharField(max_length=100, primary_key=True, serialize=False)),
                ('mpoly', django.contrib.gis.db.models.fields.PolygonField(srid=4326)),
                ('created_at', models.DateField(auto_now_add=True)),
                ('updated_at', models.DateField(auto_now=True)),
            ],
        ),
        migrations.AddField(
            model_name='cesiumentity',
            name='zone_id',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='swsite.ZoneEntity'),
        ),
    ]

2 个答案:

答案 0 :(得分:1)

如果您可以手动执行此操作(添加一个字段和一个表),则可以在迁移表中将迁移设置为已完成(或者全部,或者只是导致问题的迁移)。

答案 1 :(得分:0)

这可能会帮助你:

https://www.algotech.solutions/blog/python/django-migrations-and-how-to-manage-conflicts/

由于您是该项目的单一开发人员,如果您在git仓库中推送迁移, python manage.py migrate 应该没问题。

或者您可以通过将迁移文件夹添加到.gitignore并在每次修改db时在测试服务器上运行 python manage.py makemigrations 来忽略迁移文件夹,最后您可以执行 python manage.py migrate 以反映数据库中的更改。