summaryrefslogtreecommitdiff
path: root/django/db/models/base.py
diff options
context:
space:
mode:
authorTom Forbes <tom@tomforb.es>2020-07-12 13:59:57 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-12-15 11:25:46 +0100
commitb5e12d490af3debca8c55ab3c1698189fdedbbdb (patch)
tree5fe3005ac567f3addf78b81ae033191e2fa642f4 /django/db/models/base.py
parentb960e4ed722a04a9db0d35293f76e253eedf9126 (diff)
Fixed #31007 -- Allowed specifying type of auto-created primary keys.
This also changes the default type of auto-created primary keys for new apps and projects to BigAutoField.
Diffstat (limited to 'django/db/models/base.py')
-rw-r--r--django/db/models/base.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/django/db/models/base.py b/django/db/models/base.py
index de044886c3..822aad080d 100644
--- a/django/db/models/base.py
+++ b/django/db/models/base.py
@@ -1290,11 +1290,36 @@ class Model(metaclass=ModelBase):
*cls._check_indexes(databases),
*cls._check_ordering(),
*cls._check_constraints(databases),
+ *cls._check_default_pk(),
]
return errors
@classmethod
+ def _check_default_pk(cls):
+ if (
+ cls._meta.pk.auto_created and
+ not settings.is_overridden('DEFAULT_AUTO_FIELD') and
+ not cls._meta.app_config._is_default_auto_field_overridden
+ ):
+ return [
+ checks.Warning(
+ f"Auto-created primary key used when not defining a "
+ f"primary key type, by default "
+ f"'{settings.DEFAULT_AUTO_FIELD}'.",
+ hint=(
+ f"Configure the DEFAULT_AUTO_FIELD setting or the "
+ f"{cls._meta.app_config.__class__.__qualname__}."
+ f"default_auto_field attribute to point to a subclass "
+ f"of AutoField, e.g. 'django.db.models.BigAutoField'."
+ ),
+ obj=cls,
+ id='models.W042',
+ ),
+ ]
+ return []
+
+ @classmethod
def _check_swappable(cls):
"""Check if the swapped model exists."""
errors = []