mirror of
https://github.com/django/django.git
synced 2025-10-27 07:36:08 +00:00
Fixed #21961 -- Added support for database-level delete options for ForeignKey.
Thanks Simon Charette for pair programming. Co-authored-by: Nick Stefan <NickStefan12@gmail.com> Co-authored-by: Akash Kumar Sen <71623442+Akash-Kumar-Sen@users.noreply.github.com> Co-authored-by: Simon Charette <charette.s@gmail.com>
This commit is contained in:
@@ -3062,3 +3062,64 @@ class ConstraintsTests(TestCase):
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@isolate_apps("invalid_models_tests")
|
||||
class RelatedFieldTests(SimpleTestCase):
|
||||
def test_on_delete_python_db_variants(self):
|
||||
class Artist(models.Model):
|
||||
pass
|
||||
|
||||
class Album(models.Model):
|
||||
artist = models.ForeignKey(Artist, models.CASCADE)
|
||||
|
||||
class Song(models.Model):
|
||||
album = models.ForeignKey(Album, models.RESTRICT)
|
||||
artist = models.ForeignKey(Artist, models.DB_CASCADE)
|
||||
|
||||
self.assertEqual(
|
||||
Song.check(databases=self.databases),
|
||||
[
|
||||
Error(
|
||||
"The model cannot have related fields with both database-level and "
|
||||
"Python-level on_delete variants.",
|
||||
obj=Song,
|
||||
id="models.E050",
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
def test_on_delete_python_db_variants_auto_created(self):
|
||||
class SharedModel(models.Model):
|
||||
pass
|
||||
|
||||
class Parent(models.Model):
|
||||
pass
|
||||
|
||||
class Child(SharedModel):
|
||||
parent = models.ForeignKey(Parent, on_delete=models.DB_CASCADE)
|
||||
|
||||
self.assertEqual(
|
||||
Child.check(databases=self.databases),
|
||||
[
|
||||
Error(
|
||||
"The model cannot have related fields with both database-level and "
|
||||
"Python-level on_delete variants.",
|
||||
obj=Child,
|
||||
id="models.E050",
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
def test_on_delete_db_do_nothing(self):
|
||||
class Artist(models.Model):
|
||||
pass
|
||||
|
||||
class Album(models.Model):
|
||||
artist = models.ForeignKey(Artist, models.CASCADE)
|
||||
|
||||
class Song(models.Model):
|
||||
album = models.ForeignKey(Album, models.DO_NOTHING)
|
||||
artist = models.ForeignKey(Artist, models.DB_CASCADE)
|
||||
|
||||
self.assertEqual(Song.check(databases=self.databases), [])
|
||||
|
||||
Reference in New Issue
Block a user