回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我最近签出了一个项目的主分支,有一些模型更改尚未反映在迁移中:</p>
<pre><code>(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.
</code></pre>
<p>按照说明,我运行<code>makemigrations</code>来创建它们:</p>
^{pr2}$
<p>有趣的是,<code>0009_auto_20180425_1129.py</code>迁移是在包含Django的源代码(版本1.11.9)的<code>venv</code>中创建的,我不认为我们团队中的任何人都改变了这一点。以下是迁移:</p>
<pre><code># -*- 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'),
),
]
</code></pre>
<p>它看起来“足够无辜”,但当我尝试迁移时,我得到了以下结果<code>ProgrammingError</code>:</p>
<pre><code>(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
</code></pre>
<p>在<a href="https://stackoverflow.com/questions/29830928/django-db-utils-programmingerror-relation-already-exists">django.db.utils.ProgrammingError: relation already exists</a>处的一些答案似乎相当激烈,例如删除所有迁移或使用命令选项<a href="https://docs.djangoproject.com/en/1.9/ref/django-admin/#cmdoption-migrate-fake" rel="nofollow noreferrer">^{<cd5>}</a>,但没有解释导致错误的根本原因。在</p>
<p>你知道怎么解决这个错误吗?在</p>