summaryrefslogtreecommitdiff
path: root/django/db/backends/sqlite3
diff options
context:
space:
mode:
authorBendeguz Csirmaz <csirmazbendeguz@gmail.com>2024-04-07 10:32:16 +0800
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-11-29 11:23:04 +0100
commit978aae4334fa71ba78a3e94408f0f3aebde8d07c (patch)
treedd1cc322769441a3dd28b952ce52e07c3f72f90a /django/db/backends/sqlite3
parent86661f2449fb0903f72b3522c68e146934013377 (diff)
Fixed #373 -- Added CompositePrimaryKey.
Thanks Lily Foote and Simon Charette for reviews and mentoring this Google Summer of Code 2024 project. Co-authored-by: Simon Charette <charette.s@gmail.com> Co-authored-by: Lily Foote <code@lilyf.org>
Diffstat (limited to 'django/db/backends/sqlite3')
-rw-r--r--django/db/backends/sqlite3/schema.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py
index c5b428fc67..6da9852282 100644
--- a/django/db/backends/sqlite3/schema.py
+++ b/django/db/backends/sqlite3/schema.py
@@ -6,7 +6,7 @@ from django.db import NotSupportedError
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.backends.ddl_references import Statement
from django.db.backends.utils import strip_quotes
-from django.db.models import NOT_PROVIDED, UniqueConstraint
+from django.db.models import NOT_PROVIDED, CompositePrimaryKey, UniqueConstraint
class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
@@ -104,6 +104,13 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
f.name: f.clone() if is_self_referential(f) else f
for f in model._meta.local_concrete_fields
}
+
+ # Since CompositePrimaryKey is not a concrete field (column is None),
+ # it's not copied by default.
+ pk = model._meta.pk
+ if isinstance(pk, CompositePrimaryKey):
+ body[pk.name] = pk.clone()
+
# Since mapping might mix column names and default values,
# its values must be already quoted.
mapping = {
@@ -296,6 +303,12 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# Special-case implicit M2M tables.
if field.many_to_many and field.remote_field.through._meta.auto_created:
self.create_model(field.remote_field.through)
+ elif isinstance(field, CompositePrimaryKey):
+ # If a CompositePrimaryKey field was added, the existing primary key field
+ # had to be altered too, resulting in an AddField, AlterField migration.
+ # The table cannot be re-created on AddField, it would result in a
+ # duplicate primary key error.
+ return
elif (
# Primary keys and unique fields are not supported in ALTER TABLE
# ADD COLUMN.