[英]Django missing a migration in the 'auth' app
我正在嘗試在與我通常使用的計算機不同的計算機上處理 Django 項目。 但是,當嘗試運行以下遷移時:
from django.conf import settings
import django.contrib.postgres.fields
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('auth', '0010_auto_20180727_1345'),
('lucy_web', '0183_auto_20180814_1505'),
]
operations = [
migrations.CreateModel(
name='GoogleCredentials',
fields=[
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('user', models.OneToOneField(limit_choices_to={'is_staff': True}, on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)),
('token', models.CharField(max_length=255, null=True)),
('refresh_token', models.CharField(max_length=255, null=True)),
('token_uri', models.CharField(max_length=255, null=True)),
('client_id', models.CharField(max_length=255, null=True)),
('client_secret', models.CharField(max_length=255, null=True)),
('scopes', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=255), null=True, size=None)),
],
options={
'abstract': False,
},
),
]
我收到此錯誤消息:
django.db.migrations.exceptions.NodeNotFoundError:遷移 lucy_web.0184_googlecredentials 依賴項引用不存在的父節點('auth'、'0010_auto_20180727_1345')
在https://groups.google.com/forum/#!topic/django-users/m59NufO1GI8之后,我進入django/contrib/auth/migrations
目錄,發現確實有 9 次遷移,但第 10 次遷移是不在那里:
Kurts-MacBook-Pro:auth kurtpeek$ ls migrations
0001_initial.py
0002_alter_permission_name_max_length.py
0003_alter_user_email_max_length.py
0004_alter_user_username_opts.py
0005_alter_user_last_login_null.py
0006_require_contenttypes_0002.py
0007_alter_validators_add_error_messages.py
0008_alter_user_username_max_length.py
0009_alter_user_last_name_max_length.py
__init__.py
__pycache__/
Kurts-MacBook-Pro:auth kurtpeek$ pwd
/Users/kurtpeek/.local/share/virtualenvs/lucy-web-oCa8O1zi/lib/python3.7/site-packages/django/contrib/auth
問題是,我沒有將虛擬環境檢查到版本控制中,而且我目前無法訪問另一台計算機。 不過,我也覺得我不應該檢查項目的 Django 源代碼。
我的問題是:這種情況是如何發生的? 我懷疑它與項目中自定義django.contrib.auth.User
模型的方式有關。 我們有一個lucy_web/models/user.py
,其內容類似於以下內容:
from django.contrib.auth.models import User
from django.contrib.auth.signals import user_logged_in, user_logged_out, user_login_failed
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver
from auditlog.registry import auditlog
from lucy_web.models import LucyGuide
from crequest.middleware import CrequestMiddleware
from lucy_web.lib import intercom
# overrides built in user to provide reasonable unique constraints
User._meta.get_field('username')._unique = True
# TODO(kayla): fix this, the unique constraint on email doesn't seem to be working in prod
# I suspect the username unique constraint only works because it's the default setting
User._meta.get_field('email')._unique = True
auditlog.register(User)
@property
def family(self):
if hasattr(self, 'employee_family'):
return self.employee_family
elif hasattr(self, 'partner_family'):
return self.partner_family
else:
return None
@property
def is_employee(self):
return hasattr(self, 'employee_family')
@property
def user_apn(self):
return self.userapn_set.order_by("created_at").last()
@property
def lucy_guide(self):
try:
return self.lucyguide
except LucyGuide.DoesNotExist:
return None
def user__str__(self):
"""
User model's __str__ method.
We use a different name than '__str__' because dunder names
are reserved by Python and subject to breakage without warning
(cf. https://www.python.org/dev/peps/pep-0562/#backwards-compatibility-and-impact-on-performance).
"""
return f"{self.first_name} {self.last_name}".strip()
@property
def profile(self):
if hasattr(self, 'user_profile'):
return self.user_profile
else:
return None
@property
def using_app(self):
if hasattr(self, 'user_profile'):
return self.user_profile.using_app
return False
@property
def activation_code(self):
if hasattr(self, 'user_profile'):
return self.user_profile.activation_code
return False
User.add_to_class('family', family)
User.add_to_class('is_employee', is_employee)
User.add_to_class('user_apn', user_apn)
User.add_to_class('lucy_guide', lucy_guide)
User.add_to_class('__str__', user__str__)
User.add_to_class('profile', profile)
User.add_to_class('using_app', using_app)
簡而言之,我們使用add_to_class
方法向 Django 的User
模型添加屬性。 這似乎不是https://docs.djangoproject.com/en/2.1/topics/auth/customizing/#extending-user 中描述的推薦方法之一。 這可能是問題的根源嗎?
如果您在 Django User
模型中使用add_to_class
,它將檢測外部包中的更改並將遷移應用到外部包遷移目錄中(通常在您的虛擬環境中)。
如果稍后要創建的遷移需要User
模型, lucy_web.0184_googlecredentials
在頂部提供的遷移文件 ( lucy_web.0184_googlecredentials
) 中引用。 選擇的用戶遷移編號是創建時最后一個可用的編號,以確保兼容性。
作為一個糟糕的解決方法,您可以在應該創建新用戶模型模型遷移的新計算機上進行遷移。 找到文件名並在遷移文件lucy_web.0184_googlecredentials
臨時更改它
我還使用了add_to_class()
並在同一環境中創建了新項目。 它需要刪除環境中子文件夾“遷移”中的所有遷移和 *.pyc。 只留下__init__.py
文件。
...\Envs\blah-blah-env\Lib\site-packages\django\contrib\...
例如路徑(其中之一):
c:\Users\user\Envs\blah-blah-env\Lib\site-packages\django\contrib\auth\migrations\
如果你得到( 來源):
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency account.0001_initial on database 'default'
刪除數據庫或清除遷移表,刪除項目中的遷移文件。 祝你好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.