summaryrefslogtreecommitdiff
path: root/tests/invalid_models_tests
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2025-01-01 10:23:56 +0100
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-01-02 13:06:47 +0100
commitb322319f9d779b8726436421daae2862a380061d (patch)
tree80d1511e3ddd12e36b672093c1e0752eb0babb33 /tests/invalid_models_tests
parent2a61b5f97c0f9ec9ff3f321090bd8e6ed609793c (diff)
Fixed #36034 -- Added system check for ForeignKey/ForeignObject/ManyToManyField to CompositePrimaryKeys.
Diffstat (limited to 'tests/invalid_models_tests')
-rw-r--r--tests/invalid_models_tests/test_relative_fields.py129
1 files changed, 129 insertions, 0 deletions
diff --git a/tests/invalid_models_tests/test_relative_fields.py b/tests/invalid_models_tests/test_relative_fields.py
index 9b69ae4151..4167e0712a 100644
--- a/tests/invalid_models_tests/test_relative_fields.py
+++ b/tests/invalid_models_tests/test_relative_fields.py
@@ -440,6 +440,84 @@ class RelativeFieldTests(SimpleTestCase):
],
)
+ def test_foreignkey_to_model_with_composite_primary_key(self):
+ class Parent(models.Model):
+ pk = models.CompositePrimaryKey("version", "name")
+ version = models.IntegerField()
+ name = models.CharField(max_length=20)
+
+ class Child(models.Model):
+ rel_class_parent = models.ForeignKey(
+ Parent, on_delete=models.CASCADE, related_name="child_class_set"
+ )
+ rel_string_parent = models.ForeignKey(
+ "Parent", on_delete=models.CASCADE, related_name="child_string_set"
+ )
+
+ field = Child._meta.get_field("rel_string_parent")
+ self.assertEqual(
+ field.check(),
+ [
+ Error(
+ "Field defines a relation to the CompositePrimaryKey of model "
+ "'Parent' which is not supported.",
+ obj=field,
+ id="fields.E347",
+ ),
+ ],
+ )
+ field = Child._meta.get_field("rel_class_parent")
+ self.assertEqual(
+ field.check(),
+ [
+ Error(
+ "Field defines a relation to the CompositePrimaryKey of model "
+ "'Parent' which is not supported.",
+ obj=field,
+ id="fields.E347",
+ ),
+ ],
+ )
+
+ def test_many_to_many_to_model_with_composite_primary_key(self):
+ class Parent(models.Model):
+ pk = models.CompositePrimaryKey("version", "name")
+ version = models.IntegerField()
+ name = models.CharField(max_length=20)
+
+ class Child(models.Model):
+ rel_class_parent = models.ManyToManyField(
+ Parent, related_name="child_class_set"
+ )
+ rel_string_parent = models.ManyToManyField(
+ "Parent", related_name="child_string_set"
+ )
+
+ field = Child._meta.get_field("rel_string_parent")
+ self.assertEqual(
+ field.check(from_model=Child),
+ [
+ Error(
+ "Field defines a relation to the CompositePrimaryKey of model "
+ "'Parent' which is not supported.",
+ obj=field,
+ id="fields.E347",
+ ),
+ ],
+ )
+ field = Child._meta.get_field("rel_class_parent")
+ self.assertEqual(
+ field.check(from_model=Child),
+ [
+ Error(
+ "Field defines a relation to the CompositePrimaryKey of model "
+ "'Parent' which is not supported.",
+ obj=field,
+ id="fields.E347",
+ ),
+ ],
+ )
+
def test_foreign_key_to_non_unique_field(self):
class Target(models.Model):
bad = models.IntegerField() # No unique=True
@@ -939,6 +1017,57 @@ class RelativeFieldTests(SimpleTestCase):
],
)
+ def test_to_fields_with_composite_primary_key(self):
+ class Parent(models.Model):
+ pk = models.CompositePrimaryKey("version", "name")
+ version = models.IntegerField()
+ name = models.CharField(max_length=20)
+
+ class Child(models.Model):
+ a = models.IntegerField()
+ b = models.IntegerField()
+ parent = models.ForeignObject(
+ Parent,
+ on_delete=models.SET_NULL,
+ from_fields=("a", "b"),
+ to_fields=("pk", "version"),
+ )
+
+ field = Child._meta.get_field("parent")
+ self.assertEqual(
+ field.check(),
+ [
+ Error(
+ "Field defines a relation to the CompositePrimaryKey of model "
+ "'Parent' which is not supported.",
+ obj=field,
+ id="fields.E347",
+ ),
+ ],
+ )
+
+ def test_to_field_to_composite_primery_key(self):
+ class Parent(models.Model):
+ pk = models.CompositePrimaryKey("version", "name")
+ version = models.IntegerField()
+ name = models.CharField(max_length=20)
+
+ class Child(models.Model):
+ parent = models.ForeignKey(Parent, on_delete=models.CASCADE, to_field="pk")
+
+ field = Child._meta.get_field("parent")
+ self.assertEqual(
+ field.check(),
+ [
+ Error(
+ "Field defines a relation to the CompositePrimaryKey of model "
+ "'Parent' which is not supported.",
+ obj=field,
+ id="fields.E347",
+ ),
+ ],
+ )
+
def test_invalid_related_query_name(self):
class Target(models.Model):
pass