summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2025-01-01 16:05:22 -0500
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-01-02 13:03:49 +0100
commit2a61b5f97c0f9ec9ff3f321090bd8e6ed609793c (patch)
treea9841b3122f618919419f46b8ec2ecf4220ce234
parent8d9901c961bf9d5cfa6bddddbbcebfbf487a5125 (diff)
Refs #373 -- Errored when providing db_column to CompositePrimaryKey.
-rw-r--r--django/db/models/fields/composite.py2
-rw-r--r--tests/composite_pk/test_checks.py5
2 files changed, 7 insertions, 0 deletions
diff --git a/django/db/models/fields/composite.py b/django/db/models/fields/composite.py
index 550a440dcf..2b196f6d2a 100644
--- a/django/db/models/fields/composite.py
+++ b/django/db/models/fields/composite.py
@@ -56,6 +56,8 @@ class CompositePrimaryKey(Field):
raise ValueError("CompositePrimaryKey cannot have a default.")
if kwargs.get("db_default", NOT_PROVIDED) is not NOT_PROVIDED:
raise ValueError("CompositePrimaryKey cannot have a database default.")
+ if kwargs.get("db_column", None) is not None:
+ raise ValueError("CompositePrimaryKey cannot have a db_column.")
if kwargs.setdefault("editable", False):
raise ValueError("CompositePrimaryKey cannot be editable.")
if not kwargs.setdefault("primary_key", True):
diff --git a/tests/composite_pk/test_checks.py b/tests/composite_pk/test_checks.py
index 58b580ca85..c803d521cc 100644
--- a/tests/composite_pk/test_checks.py
+++ b/tests/composite_pk/test_checks.py
@@ -43,6 +43,11 @@ class CompositePKChecksTests(TestCase):
with self.assertRaisesMessage(ValueError, expected_message):
models.CompositePrimaryKey("tenant_id", "id", db_default=models.F("id"))
+ def test_composite_pk_cannot_have_a_db_column(self):
+ expected_message = "CompositePrimaryKey cannot have a db_column."
+ with self.assertRaisesMessage(ValueError, expected_message):
+ models.CompositePrimaryKey("tenant_id", "id", db_column="tenant_pk")
+
def test_composite_pk_cannot_be_editable(self):
expected_message = "CompositePrimaryKey cannot be editable."
with self.assertRaisesMessage(ValueError, expected_message):