summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2018-05-03 11:38:29 +0430
committerCarlton Gibson <carlton.gibson@noumenal.es>2018-05-03 09:08:29 +0200
commit816b8d9518c41f034dcbacfd1f1826f2366975e5 (patch)
tree997a10e0626c8aa06631a7056ef8ff5d56e33edc /django
parent98019df855fb8fb93e4e9505afeedcad29da3125 (diff)
Fixed #29358 -- Added a system check to prohibit models with more than one primary_key field.
Diffstat (limited to 'django')
-rw-r--r--django/db/models/base.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/django/db/models/base.py b/django/db/models/base.py
index 9af6a8f92d..3573838b99 100644
--- a/django/db/models/base.py
+++ b/django/db/models/base.py
@@ -1189,6 +1189,7 @@ class Model(metaclass=ModelBase):
*cls._check_field_name_clashes(),
*cls._check_model_name_db_lookup_clashes(),
*cls._check_property_name_related_field_accessor_clashes(),
+ *cls._check_single_primary_key(),
)
errors.extend(clash_errors)
# If there are field name clashes, hide consequent column name
@@ -1437,6 +1438,19 @@ class Model(metaclass=ModelBase):
return errors
@classmethod
+ def _check_single_primary_key(cls):
+ errors = []
+ if sum(1 for f in cls._meta.local_fields if f.primary_key) > 1:
+ errors.append(
+ checks.Error(
+ "Model can not contain more than one 'primary_key' field.",
+ obj=cls,
+ id='models.E026',
+ )
+ )
+ return errors
+
+ @classmethod
def _check_index_together(cls):
"""Check the value of "index_together" option."""
if not isinstance(cls._meta.index_together, (tuple, list)):