mirror of
https://github.com/django/django.git
synced 2025-10-28 08:06:09 +00:00
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:
committed by
Simon Charette
parent
26d118c3fe
commit
956bd64424
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user