Django编程错误:在Django源代码中创建迁移后,关系已经存在?

2024-04-28 19:49:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

(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.

按照说明,我运行makemigrations来创建它们:

^{pr2}$

有趣的是,0009_auto_20180425_1129.py迁移是在包含Django的源代码(版本1.11.9)的venv中创建的,我不认为我们团队中的任何人都改变了这一点。以下是迁移:

# -*- 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'),
        ),
    ]

它看起来“足够无辜”,但当我尝试迁移时,我得到了以下结果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...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

django.db.utils.ProgrammingError: relation already exists处的一些答案似乎相当激烈,例如删除所有迁移或使用命令选项^{},但没有解释导致错误的根本原因。在

你知道怎么解决这个错误吗?在


Tags: todjangopyauthwebdbmanagevenv
2条回答

事实证明,auth_user_email_1c89df09_uniq关系实际上是一个约束(所以不是数据)。我通过简单地删除/删除pgAdmin中的这个约束就成功地进行了迁移,并且对auth_user_email_1c89df09_like索引(之后弹出了一个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表中:

enter image description here

试试这个,这会有用的:

注意:此字段中的所有数据都将丢失

在运行了最后一次迁移之后,您就有了这个文件0009_auto_20180425_1129.py 正在等待迁移。。。如果不再有此文件,请重新运行makemigrations,让最后一个迁移文件等待migrate。在

在您的案例中,浏览该文件0009_auto_20180425_1129.py,然后进入其中 operations

I suppose you don't have any data in db

添加以下行:

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'   
),

feel free to comment what you get after

相关问题 更多 >