mirror of
https://github.com/django/django.git
synced 2025-10-24 06: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
47927eb786
commit
00e3b9a2a9
@@ -291,3 +291,33 @@ class StateTests(TestCase):
|
||||
))
|
||||
self.assertNotEqual(project_state, other_state)
|
||||
self.assertEqual(project_state == other_state, False)
|
||||
|
||||
def test_dangling_references_throw_error(self):
|
||||
class Author(models.Model):
|
||||
name = models.TextField()
|
||||
|
||||
class Book(models.Model):
|
||||
author = models.ForeignKey(Author)
|
||||
|
||||
class Magazine(models.Model):
|
||||
authors = models.ManyToManyField(Author)
|
||||
|
||||
# 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