当前位置: 首页>>技术教程>>正文


python – django.db.utils.ProgrammingError:关系已经存在

, , ,

问题描述

我正在尝试为新的django项目设置表(也就是说,数据库中不存在这些表); django版本是1.7,数据库后端是PostgreSQL。该项目的名称是crud。迁移尝试的结果如下:

python manage.py makemigrations crud

Migrations for 'crud':
  0001_initial.py:
    - Create model AddressPoint
    - Create model CrudPermission
    - Create model CrudUser
    - Create model LDAPGroup
    - Create model LogEntry
    - Add field ldap_groups to cruduser
    - Alter unique_together for crudpermission (1 constraint(s))

python manage.py migrate crud

Operations to perform:
  Apply all migrations: crud
Running migrations:
  Applying crud.0001_initial...Traceback (most recent call last):
  File "manage.py", line 18, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 108, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/models.py", line 36, in database_forwards
    schema_editor.create_model(model)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 262, in create_model
    self.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 103, in execute
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 82, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 66, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 66, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "crud_crudpermission" already exists

迁移文件中的一些要点:

dependencies = [
    ('auth', '0001_initial'),
    ('contenttypes', '0001_initial'),
]
    migrations.CreateModel(
        name='CrudPermission',
        fields=[
            ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
            ('_created_by', models.CharField(default=b'', max_length=64, null=True, editable=False, blank=True)),
            ('_last_updated_by', models.CharField(default=b'', max_length=64, null=True, editable=False, blank=True)),
            ('_created', models.DateTimeField(null=True, editable=False, blank=True)),
            ('_last_updated', models.DateTimeField(null=True, editable=False, blank=True)),
            ('domain', models.CharField(max_length=32, choices=[(b'town', b'Town'), (b'boe', b'BOE'), (b'police', b'Police')])),
            ('ldap_group', models.CharField(max_length=128, verbose_name=b'LDAP group')),
            ('can_add', models.BooleanField(default=False, verbose_name=b'add')),
            ('can_change', models.BooleanField(default=False, verbose_name=b'change')),
            ('restrict_change_to_own', models.BooleanField(default=False)),
            ('can_delete', models.BooleanField(default=False, verbose_name=b'delete')),
            ('restrict_delete_to_own', models.BooleanField(default=False)),
            ('models', models.ManyToManyField(to='contenttypes.ContentType', null=True, blank=True)),
        ],
        options={
            'verbose_name': 'CRUD permission',
        },
        bases=(models.Model,),
    ),
    migrations.AlterUniqueTogether(
        name='crudpermission',
        unique_together=set([('ldap_group', 'can_add', 'can_change', 'can_delete', 'domain')]),
    )

Crud应用程序实际上并不意味着要执行任何操作,但是我将其用于其他应用程序,因此当我尝试从该应用程序迁移时,会触发上述问题。

我在网络上也发现了类似问题的其他示例,但似乎没有一个案例适用,因为

  1. 该问题影响到整个关系,而不仅仅是一列

  2. 我没有使用多重继承。

我应该下一步去寻找潜在的问题?

最佳方案

这个工作很好

./manage.py migrate --fake default

资料来源:-https://github.com/nijel/weblate/issues/587

次佳方案

最初执行python manage.py migrate --fake

阅读https://docs.djangoproject.com/en/1.9/ref/django-admin/#django-admin-migrate

第三种方案

面临类似的问题,最终删除了迁移文件夹中的所有.py文件(django 1.7自动创建了一个),此后运行完美。

第四种方案

向现有模型添加几个新字段时,我也遇到了类似的问题。我使用的是Django 1.9,即introduced --run-syncdb选项。运行manage.py migrate --run-syncdb修复了我的表。

第五种方案

我遇到了类似的问题,在这里我更改了列名。我收到与他的问题一起提供的stack-trace中提到的错误。

这就是我所做的。

我先进行假迁移。然后我从django_migrations表中删除了它(我想运行的迁移)条目,然后再次运行迁移(这次没有伪造)。

出现了预期的变化。

希望这会有所帮助。

第六种方案

现在(我正在使用Django 1.9),您可以执行以下操作:

./manage.py [-数据库数据库] –fake [app_label] [migration_name]

这样,您可以更准确地定位问题,并且只能在特定数据库上伪造有问题的迁移。

因此,查看问题,您可以:

./manage.py-数据库默认值–fake crud crud.0001_initial

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/9131.html,未经允许,请勿转载。