diff options
| author | jkhall81 <jason.kei.hall@gmail.com> | 2025-07-28 08:59:07 -0700 |
|---|---|---|
| committer | nessita <124304+nessita@users.noreply.github.com> | 2025-08-05 08:34:40 -0300 |
| commit | 2013092b693be0ebdf36f41dc61615a2de1bbe31 (patch) | |
| tree | 1fb5031596c477a7aa8ea87b4dd0e27b6961f918 /django/db | |
| parent | e664c5afa934be5a7b57576de12620071077c4fb (diff) | |
Fixed #36530 -- Extended fields.E347 to check for ManyToManyField involving CompositePrimaryKey on either side.
Thanks to Jacob Walls for the report.
Diffstat (limited to 'django/db')
| -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 a59dcac68c..9ad440128e 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -626,9 +626,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", ) @@ -1538,20 +1539,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 |
