diff options
| author | jkhall81 <jason.kei.hall@gmail.com> | 2025-07-28 08:59:07 -0700 |
|---|---|---|
| committer | Natalia <124304+nessita@users.noreply.github.com> | 2025-08-05 08:46:56 -0300 |
| commit | bdc3f9e3508fc144c5e9710f5b672cc41f6e742d (patch) | |
| tree | f42ec688c25eff8c0ae30632131da063665a0386 /django | |
| parent | f01ceae477a71a1c244c332e1b53a9499e484874 (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 'django')
| -rw-r--r-- | django/db/models/fields/related.py | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index e873e5ca86..fcb2ebe524 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -627,9 +627,10 @@ class ForeignObject(RelatedField): if isinstance(field, CompositePrimaryKey): errors.append( checks.Error( - "Field defines a relation to the CompositePrimaryKey of " - f"model {self.remote_field.model._meta.object_name!r} " - "which is not supported.", + "Field defines a relation involving model " + f"{self.remote_field.model._meta.object_name!r} which has " + "a CompositePrimaryKey and such relations are not " + "supported.", obj=self, id="fields.E347", ) @@ -1539,20 +1540,24 @@ class ManyToManyField(RelatedField): to_model_name = to_model else: to_model_name = to_model._meta.object_name - if ( - self.remote_field.through_fields is None - and not isinstance(to_model, str) - and isinstance(to_model._meta.pk, CompositePrimaryKey) + if self.remote_field.through_fields is None and not isinstance( + to_model, str ): - errors.append( - checks.Error( - "Field defines a relation to the CompositePrimaryKey of model " - f"{self.remote_field.model._meta.object_name!r} which is not " - "supported.", - obj=self, - id="fields.E347", + model_name = None + if isinstance(to_model._meta.pk, CompositePrimaryKey): + model_name = self.remote_field.model._meta.object_name + elif isinstance(from_model._meta.pk, CompositePrimaryKey): + model_name = from_model_name + if model_name: + errors.append( + checks.Error( + f"Field defines a relation involving model {model_name!r} " + "which has a CompositePrimaryKey and such relations are " + "not supported.", + obj=self, + id="fields.E347", + ) ) - ) relationship_model_name = self.remote_field.through._meta.object_name self_referential = from_model == to_model # Count foreign keys in intermediate model |
