Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django ProgrammingError:在Django源代码中创建迁移后,关系是否已存在?_Python_Django - Fatal编程技术网

Python Django ProgrammingError:在Django源代码中创建迁移后,关系是否已存在?

Python Django ProgrammingError:在Django源代码中创建迁移后,关系是否已存在?,python,django,Python,Django,我最近签出了一个项目的主分支,迁移中还没有反映模型更改: (venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py migrate Operations to perform: Apply all migrations: admin, auditlog, auth, contenttypes, lucy_web, oauth2_provider, otp_static, otp_totp, sessions, two_fac

我最近签出了一个项目的主分支,迁移中还没有反映模型更改:

(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auditlog, auth, contenttypes, lucy_web, oauth2_provider, otp_static, otp_totp, sessions, two_factor
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.
# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-04-25 18:29
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('auth', '0008_alter_user_username_max_length'),
    ]

    operations = [
        migrations.AlterField(
            model_name='user',
            name='email',
            field=models.EmailField(blank=True, max_length=254, unique=True, verbose_name='email address'),
        ),
    ]
按照说明,我运行了
makemigrations
来创建它们:

(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py makemigrations
Migrations for 'auth':
  venv/lib/python3.6/site-packages/django/contrib/auth/migrations/0009_auto_20180425_1129.py
    - Alter field email on user
Migrations for 'lucy_web':
  lucy_web/migrations/0146_auto_20180425_1129.py
    - Alter field description on sessiontype
    - Alter field short_description on sessiontype
有趣的是,
0009_auto_20180425_1129.py
迁移是在包含Django源代码(版本1.11.9)的
venv
中创建的,我相信我们团队中没有人更改过。以下是迁移:

(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auditlog, auth, contenttypes, lucy_web, oauth2_provider, otp_static, otp_totp, sessions, two_factor
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.
# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-04-25 18:29
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('auth', '0008_alter_user_username_max_length'),
    ]

    operations = [
        migrations.AlterField(
            model_name='user',
            name='email',
            field=models.EmailField(blank=True, max_length=254, unique=True, verbose_name='email address'),
        ),
    ]
这似乎“足够天真”,但当我尝试迁移时,我得到了以下
编程错误

(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auditlog, auth, contenttypes, lucy_web, oauth2_provider, otp_static, otp_totp, sessions, two_factor
Running migrations:
  Applying auth.0009_auto_20180425_1129...Traceback (most recent call last):
  File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "auth_user_email_1c89df09_uniq" already exists
上的一些答案似乎相当激烈,例如删除所有迁移或使用命令选项,但没有解释导致错误的根本原因


你知道如何解决这个错误吗?

试试这个,这样可以:

注意:此字段中的所有数据都将丢失 运行上一次迁移后,您拥有此文件
0009\u auto\u 20180425\u 1129.py
正在等待迁移。。。如果您不再有此文件,请重新运行
makemigrations
,使最后一个迁移文件等待
migrate

在您的情况下,请仔细阅读该文件,并将其放在文件中
0009\u auto\u 20180425\u 1129.py
操作

我想你的数据库里没有任何数据

添加以下行:

migrations.RemoveField(
    model_name='user',
    name='email',
),
migrations.AddField(
    model_name='user',
    name='email',
    field=models.EmailField(blank=True, max_length=254, unique=True, verbose_name='email address'   
),
请随意评论您的评论


事实证明,
auth\u user\u email\u 1c89df09\u uniq
关系实际上是一个约束(因此不是数据)。我通过简单地在pgAdmin中删除/删除这个约束来进行迁移,对于类似于
auth\u user\u email\u 1c89df09\u的
索引(之后会弹出一个
ProgrammingError

在此之后,我能够迁移:

(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auditlog, auth, contenttypes, lucy_web, oauth2_provider, otp_static, otp_totp, sessions, two_factor
Running migrations:
  Applying auth.0009_auto_20180425_1129... OK
  Applying lucy_web.0146_auto_20180425_1129... OK
约束和索引已放回
auth_user
表中:


makemigrations
不应在
django.contrib.auth
应用程序中生成迁移。您应该尝试找出项目中的哪些代码导致创建此迁移。如果数据库中有任何重要数据,则删除电子邮件字段并重新创建它不是一个好的建议。据我所知,数据库中没有数据。我面临相同的问题,上次迁移成功,但突然出现此错误,此解决方案对数据是否有任何副作用?