mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
[1.7.x] 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.
Backport of 956bd64424 from master
This commit is contained in:
committed by
Simon Charette
parent
b06e45bbf6
commit
bc5d568e1e
@@ -291,3 +291,44 @@ class StateTests(TestCase):
|
||||
))
|
||||
self.assertNotEqual(project_state, other_state)
|
||||
self.assertEqual(project_state == other_state, False)
|
||||
|
||||
def test_dangling_references_throw_error(self):
|
||||
new_apps = Apps()
|
||||
|
||||
class Author(models.Model):
|
||||
name = models.TextField()
|
||||
class Meta:
|
||||
app_label = "migrations"
|
||||
apps = new_apps
|
||||
|
||||
class Book(models.Model):
|
||||
author = models.ForeignKey(Author)
|
||||
class Meta:
|
||||
app_label = "migrations"
|
||||
apps = new_apps
|
||||
|
||||
class Magazine(models.Model):
|
||||
authors = models.ManyToManyField(Author)
|
||||
class Meta:
|
||||
app_label = "migrations"
|
||||
apps = new_apps
|
||||
|
||||
# Make a valid ProjectState and render it
|
||||
project_state = ProjectState()
|
||||
project_state.add_model_state(ModelState.from_model(Author))
|
||||
project_state.add_model_state(ModelState.from_model(Book))
|
||||
project_state.add_model_state(ModelState.from_model(Magazine))
|
||||
rendered_state = project_state.render()
|
||||
self.assertEqual(len(rendered_state.get_models()), 3)
|
||||
|
||||
# now make an invalid one with a ForeignKey
|
||||
project_state = ProjectState()
|
||||
project_state.add_model_state(ModelState.from_model(Book))
|
||||
with self.assertRaises(ValueError):
|
||||
rendered_state = project_state.render()
|
||||
|
||||
# and another with ManyToManyField
|
||||
project_state = ProjectState()
|
||||
project_state.add_model_state(ModelState.from_model(Magazine))
|
||||
with self.assertRaises(ValueError):
|
||||
rendered_state = project_state.render()
|
||||
|
||||
Reference in New Issue
Block a user