From fc974313b85da932a70f1f993b33207d78d31831 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 7 May 2014 13:04:29 -0700 Subject: [PATCH] Fixed #22563: Better error message when trying to change AUTH_USER_MODEL You're not allowed to do this after you've made migrations; see ticket for more details. --- django/db/migrations/state.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/django/db/migrations/state.py b/django/db/migrations/state.py index a5ea1cf37c..90e0b51b1e 100644 --- a/django/db/migrations/state.py +++ b/django/db/migrations/state.py @@ -5,6 +5,7 @@ from django.apps.registry import Apps, apps as global_apps from django.db import models from django.db.models.options import DEFAULT_NAMES, normalize_together from django.db.models.fields.related import do_pending_lookups +from django.conf import settings from django.utils import six from django.utils.encoding import force_text from django.utils.module_loading import import_string @@ -74,13 +75,23 @@ class ProjectState(object): try: model = self.apps.get_model(lookup_model[0], lookup_model[1]) except LookupError: + # If the lookup failed to something that looks like AUTH_USER_MODEL, + # give a better error message about how you can't change it (#22563) + extra_message = "" + if "%s.%s" % (lookup_model[0], lookup_model[1]) == settings.AUTH_USER_MODEL: + extra_message = ( + "\nThe missing model matches AUTH_USER_MODEL; if you've changed the value of this" + + "\nsetting after making a migration, be aware that this is not supported. If you" + + "\nchange AUTH_USER_MODEL you must delete and recreate migrations for its app." + ) # Raise an error with a best-effort helpful message # (only for the first issue). Error message should look like: # "ValueError: Lookup failed for model referenced by # field migrations.Book.author: migrations.Author" - raise ValueError("Lookup failed for model referenced by field {field}: {model[0]}.{model[1]}".format( - field=operations[0][1], - model=lookup_model + raise ValueError("Lookup failed for model referenced by field {field}: {model[0]}.{model[1]}{extra_message}".format( + field = operations[0][1], + model = lookup_model, + extra_message = extra_message, )) else: do_pending_lookups(model)