summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorjkhall81 <jason.kei.hall@gmail.com>2025-07-28 08:59:07 -0700
committerNatalia <124304+nessita@users.noreply.github.com>2025-08-05 08:46:56 -0300
commitbdc3f9e3508fc144c5e9710f5b672cc41f6e742d (patch)
treef42ec688c25eff8c0ae30632131da063665a0386 /tests
parentf01ceae477a71a1c244c332e1b53a9499e484874 (diff)
[5.2.x] Fixed #36530 -- Extended fields.E347 to check for ManyToManyField involving CompositePrimaryKey on either side.
Thanks to Jacob Walls for the report. Backport of 2013092b693be0ebdf36f41dc61615a2de1bbe31 from main.
Diffstat (limited to 'tests')
-rw-r--r--tests/invalid_models_tests/test_relative_fields.py78
1 files changed, 42 insertions, 36 deletions
diff --git a/tests/invalid_models_tests/test_relative_fields.py b/tests/invalid_models_tests/test_relative_fields.py
index 4167e0712a..f49d0a386d 100644
--- a/tests/invalid_models_tests/test_relative_fields.py
+++ b/tests/invalid_models_tests/test_relative_fields.py
@@ -454,29 +454,19 @@ class RelativeFieldTests(SimpleTestCase):
"Parent", on_delete=models.CASCADE, related_name="child_string_set"
)
+ error = (
+ "Field defines a relation involving model 'Parent' which has a "
+ "CompositePrimaryKey and such relations are not supported."
+ )
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",
- ),
- ],
+ [Error(error, 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",
- ),
- ],
+ [Error(error, obj=field, id="fields.E347")],
)
def test_many_to_many_to_model_with_composite_primary_key(self):
@@ -493,29 +483,45 @@ class RelativeFieldTests(SimpleTestCase):
"Parent", related_name="child_string_set"
)
+ error = (
+ "Field defines a relation involving model 'Parent' which has a "
+ "CompositePrimaryKey and such relations are not supported."
+ )
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",
- ),
- ],
+ [Error(error, 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",
- ),
- ],
+ [Error(error, obj=field, id="fields.E347")],
+ )
+
+ def test_many_to_many_from_model_with_composite_primary_key(self):
+ class Parent(models.Model):
+ name = models.CharField(max_length=20)
+
+ class Meta:
+ app_label = "invalid_models_tests"
+
+ class Child(models.Model):
+ pk = models.CompositePrimaryKey("version", "name")
+ version = models.IntegerField()
+ name = models.CharField(max_length=20)
+ parents = models.ManyToManyField(Parent)
+
+ class Meta:
+ app_label = "invalid_models_tests"
+
+ error = (
+ "Field defines a relation involving model 'Child' which has a "
+ "CompositePrimaryKey and such relations are not supported."
+ )
+ field = Child._meta.get_field("parents")
+ self.assertEqual(
+ field.check(from_model=Child),
+ [Error(error, obj=field, id="fields.E347")],
)
def test_foreign_key_to_non_unique_field(self):
@@ -1038,8 +1044,8 @@ class RelativeFieldTests(SimpleTestCase):
field.check(),
[
Error(
- "Field defines a relation to the CompositePrimaryKey of model "
- "'Parent' which is not supported.",
+ "Field defines a relation involving model 'Parent' which has a "
+ "CompositePrimaryKey and such relations are not supported.",
obj=field,
id="fields.E347",
),
@@ -1060,8 +1066,8 @@ class RelativeFieldTests(SimpleTestCase):
field.check(),
[
Error(
- "Field defines a relation to the CompositePrimaryKey of model "
- "'Parent' which is not supported.",
+ "Field defines a relation involving model 'Parent' which has a "
+ "CompositePrimaryKey and such relations are not supported.",
obj=field,
id="fields.E347",
),