Fixed #22397 -- Issues removing M2M field with explicit through model.

Changed the migration autodetector to remove models last so that FK
and M2M fields will not be left as dangling references. Added a check
in the migration state renderer to error out in the presence of
dangling references instead of leaving them as strings. Fixed a bug
in the sqlite backend to handle the deletion of M2M fields with
"through" models properly (i.e., do nothing successfully).

Thanks to melinath for report, loic for tests and andrewgodwin and
charettes for assistance with architecture.
This commit is contained in:
Andrew Gorcester 2014-04-08 12:30:25 +07:00 committed by Simon Charette
parent 47927eb786
commit 00e3b9a2a9
7 changed files with 160 additions and 28 deletions

View file

@ -51,6 +51,16 @@ class ProjectState(object):
if len(new_unrendered_models) == len(unrendered_models):
raise InvalidBasesError("Cannot resolve bases for %r" % new_unrendered_models)
unrendered_models = new_unrendered_models
# make sure apps has no dangling references
if self.apps._pending_lookups:
# 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"
dangling_lookup = list(self.apps._pending_lookups.items())[0]
raise ValueError("Lookup failed for model referenced by field {field}: {model[0]}.{model[1]}".format(
field=dangling_lookup[1][0][1],
model=dangling_lookup[0]))
return self.apps
@classmethod