summaryrefslogtreecommitdiff
path: root/tests/invalid_models_tests/test_models.py
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2024-12-11 21:37:23 +0100
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-12-17 10:28:40 +0100
commit2249370c8611d97f8bdb6003fb7b4d8fd3646202 (patch)
tree76e98a2f349b15e01d6c12526a835acd49a600b6 /tests/invalid_models_tests/test_models.py
parent322e49ba3071022dde96f6aae71a578a1588db33 (diff)
Fixed #35992, Fixed #35997 -- Added system check for CompositePrimaryKeys in Meta.indexes/constraints/unique_together.
CompositePrimaryKeys are not supported in any of these options.
Diffstat (limited to 'tests/invalid_models_tests/test_models.py')
-rw-r--r--tests/invalid_models_tests/test_models.py183
1 files changed, 183 insertions, 0 deletions
diff --git a/tests/invalid_models_tests/test_models.py b/tests/invalid_models_tests/test_models.py
index 8b6d705acb..c44fe56f89 100644
--- a/tests/invalid_models_tests/test_models.py
+++ b/tests/invalid_models_tests/test_models.py
@@ -145,6 +145,27 @@ class UniqueTogetherTests(SimpleTestCase):
self.assertEqual(Bar.check(), [])
+ def test_pointing_to_composite_primary_key(self):
+ class Model(models.Model):
+ pk = models.CompositePrimaryKey("version", "name")
+ version = models.IntegerField()
+ name = models.CharField(max_length=20)
+
+ class Meta:
+ unique_together = [["pk"]]
+
+ self.assertEqual(
+ Model.check(),
+ [
+ Error(
+ "'unique_together' refers to a CompositePrimaryKey 'pk', but "
+ "CompositePrimaryKeys are not permitted in 'unique_together'.",
+ obj=Model,
+ id="models.E048",
+ ),
+ ],
+ )
+
@isolate_apps("invalid_models_tests")
class IndexesTests(TestCase):
@@ -225,6 +246,27 @@ class IndexesTests(TestCase):
self.assertEqual(Bar.check(), [])
+ def test_pointing_to_composite_primary_key(self):
+ class Model(models.Model):
+ pk = models.CompositePrimaryKey("version", "name")
+ version = models.IntegerField()
+ name = models.CharField(max_length=20)
+
+ class Meta:
+ indexes = [models.Index(fields=["pk", "name"], name="name")]
+
+ self.assertEqual(
+ Model.check(),
+ [
+ Error(
+ "'indexes' refers to a CompositePrimaryKey 'pk', but "
+ "CompositePrimaryKeys are not permitted in 'indexes'.",
+ obj=Model,
+ id="models.E048",
+ ),
+ ],
+ )
+
def test_name_constraints(self):
class Model(models.Model):
class Meta:
@@ -446,6 +488,28 @@ class IndexesTests(TestCase):
self.assertEqual(Model.check(databases=self.databases), [])
+ @skipUnlessDBFeature("supports_covering_indexes")
+ def test_index_include_pointing_to_composite_primary_key(self):
+ class Model(models.Model):
+ pk = models.CompositePrimaryKey("version", "name")
+ version = models.IntegerField()
+ name = models.CharField(max_length=20)
+
+ class Meta:
+ indexes = [models.Index(fields=["name"], include=["pk"], name="name")]
+
+ self.assertEqual(
+ Model.check(),
+ [
+ Error(
+ "'indexes' refers to a CompositePrimaryKey 'pk', but "
+ "CompositePrimaryKeys are not permitted in 'indexes'.",
+ obj=Model,
+ id="models.E048",
+ ),
+ ],
+ )
+
def test_func_index(self):
class Model(models.Model):
name = models.CharField(max_length=10)
@@ -581,6 +645,27 @@ class IndexesTests(TestCase):
self.assertEqual(Bar.check(), [])
+ def test_func_index_pointing_to_composite_primary_key(self):
+ class Model(models.Model):
+ pk = models.CompositePrimaryKey("version", "name")
+ version = models.IntegerField()
+ name = models.CharField(max_length=20)
+
+ class Meta:
+ indexes = [models.Index(Abs("pk"), name="name")]
+
+ self.assertEqual(
+ Model.check(),
+ [
+ Error(
+ "'indexes' refers to a CompositePrimaryKey 'pk', but "
+ "CompositePrimaryKeys are not permitted in 'indexes'.",
+ obj=Model,
+ id="models.E048",
+ ),
+ ],
+ )
+
@isolate_apps("invalid_models_tests")
class FieldNamesTests(TestCase):
@@ -2209,6 +2294,33 @@ class ConstraintsTests(TestCase):
)
self.assertEqual(Model.check(databases=self.databases), expected_warnings)
+ @skipUnlessDBFeature("supports_table_check_constraints")
+ def test_check_constraint_pointing_to_composite_primary_key(self):
+ class Model(models.Model):
+ pk = models.CompositePrimaryKey("version", "name")
+ version = models.IntegerField()
+ name = models.CharField(max_length=20)
+
+ class Meta:
+ constraints = [
+ models.CheckConstraint(
+ name="name",
+ condition=models.Q(pk__gt=(7, "focal")),
+ ),
+ ]
+
+ self.assertEqual(
+ Model.check(databases=self.databases),
+ [
+ Error(
+ "'constraints' refers to a CompositePrimaryKey 'pk', but "
+ "CompositePrimaryKeys are not permitted in 'constraints'.",
+ obj=Model,
+ id="models.E048",
+ ),
+ ],
+ )
+
def test_unique_constraint_with_condition(self):
class Model(models.Model):
age = models.IntegerField()
@@ -2471,6 +2583,27 @@ class ConstraintsTests(TestCase):
self.assertEqual(Model.check(databases=self.databases), [])
+ def test_unique_constraint_pointing_to_composite_primary_key(self):
+ class Model(models.Model):
+ pk = models.CompositePrimaryKey("version", "name")
+ version = models.IntegerField()
+ name = models.CharField(max_length=20)
+
+ class Meta:
+ constraints = [models.UniqueConstraint(fields=["pk"], name="name")]
+
+ self.assertEqual(
+ Model.check(databases=self.databases),
+ [
+ Error(
+ "'constraints' refers to a CompositePrimaryKey 'pk', but "
+ "CompositePrimaryKeys are not permitted in 'constraints'.",
+ obj=Model,
+ id="models.E048",
+ ),
+ ],
+ )
+
def test_unique_constraint_with_include(self):
class Model(models.Model):
age = models.IntegerField()
@@ -2618,6 +2751,34 @@ class ConstraintsTests(TestCase):
self.assertEqual(Model.check(databases=self.databases), [])
+ @skipUnlessDBFeature("supports_covering_indexes")
+ def test_unique_constraint_include_pointing_to_composite_primary_key(self):
+ class Model(models.Model):
+ pk = models.CompositePrimaryKey("version", "name")
+ version = models.IntegerField()
+ name = models.CharField(max_length=20)
+
+ class Meta:
+ constraints = [
+ models.UniqueConstraint(
+ fields=["version"],
+ include=["pk"],
+ name="name",
+ ),
+ ]
+
+ self.assertEqual(
+ Model.check(databases=self.databases),
+ [
+ Error(
+ "'constraints' refers to a CompositePrimaryKey 'pk', but "
+ "CompositePrimaryKeys are not permitted in 'constraints'.",
+ obj=Model,
+ id="models.E048",
+ ),
+ ],
+ )
+
def test_func_unique_constraint(self):
class Model(models.Model):
name = models.CharField(max_length=10)
@@ -2815,3 +2976,25 @@ class ConstraintsTests(TestCase):
]
self.assertEqual(Bar.check(databases=self.databases), [])
+
+ @skipUnlessDBFeature("supports_expression_indexes")
+ def test_func_unique_constraint_pointing_composite_primary_key(self):
+ class Model(models.Model):
+ pk = models.CompositePrimaryKey("version", "name")
+ version = models.IntegerField()
+ name = models.CharField(max_length=20)
+
+ class Meta:
+ constraints = [models.UniqueConstraint(Abs("pk"), name="name")]
+
+ self.assertEqual(
+ Model.check(databases=self.databases),
+ [
+ Error(
+ "'constraints' refers to a CompositePrimaryKey 'pk', but "
+ "CompositePrimaryKeys are not permitted in 'constraints'.",
+ obj=Model,
+ id="models.E048",
+ ),
+ ],
+ )