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

我试图build立一个新的Django项目的表(即表中不存在的数据库); django版本是1.7,而db后端是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应用程序并不意味着实际上做任何事情,但我使用它的另一个应用程序,所以当我尝试从该应用程序迁移,我触发上述问题。

我在networking上发现了类似问题的networking上的其他例子,但他们的案例似乎都没有适用,因为

  1. 问题影响整个关系,而不仅仅是一列
  2. 我不使用多重inheritance。

我应该在哪里寻找下面的问题?

这工作很好

 ./manage.py migrate --fake default 

来源: – https://github.com/nijel/weblate/issues/587

面对类似的问题,最终删除了迁移文件夹中的所有.py文件(django 1.7自动创build一个),之后完美工作。

做“python manage.py migrate –fake-inital。阅读https://docs.djangoproject.com/en/1.9/ref/django-admin/#django-admin-migrate

将几个新字段添加到现有模型时遇到了类似的问题。 我正在使用Django 1.9,它引入了 --run-syncdb选项。 运行manage.py migrate --run-syncdb修复了我的表。

我正面临类似的问题,我已经改变了列名。 我得到了与他提出的堆栈跟踪中提到的相同的错误。

这是我做的。

我首先跑假冒的移民。 然后,我从django_migrations表中删除了它(我想运行的迁移)条目,并再次运行迁移(这次没有假冒)。

对我来说,变化如预期般出现。

希望这是有帮助的。

我在Django 1.10项目中find并解决了这个错误的一个特殊例子,当时我将一个名为member的外键字段改为指向一个不同的表。 我正在改变这三种不同的模式,我打这个错误。 在我第一次尝试我将member重命名为member_user并试图创build一个新的字段member作为指向新表的外键,但是这不起作用。

我发现,当我重命名member列时,它并没有修改<app>_<model>_<hash>forms的索引名,当我尝试创build一个新的member列时,它试图创build相同的索引名因为名字的哈希部分是相同的。

我通过临时创build一个新的member_user关系并复制数据解决了这个问题。 这创build了一个不同的哈希值的新索引。 然后,我删除了member并重新创build它指向新的表,并与它将矛盾的索引名称。 一旦完成,我运行RunPython步骤来填充新member列,并引用适用的表。 我完成了通过添加RemoveField迁移来清理临时member_user列。

我不得不把我的迁移分成两个文件,因为我收到这个错误:

psycopg2.OperationalError:不能ALTER TABLE“<table_name>”,因为它有未决的触发事件

在将数据创build并复制到member_user我无法在相同的迁移事务中删除member 。 这可能是postgres特定的限制,但是通过创build另一个事务并在创build和复制member_user所有内容移动到第二个迁移中,很容易解决这个问题。

现在(我使用Django 1.9),你可以:

./manage.py [–database DATABASE] –fake [app_label] [migration_name]

通过这种方式,您可以更准确地定位问题,并且只能伪造特定数据库上存在问题的迁移。

所以,看这个问题,你可以:

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