summaryrefslogtreecommitdiff
path: root/tests/composite_pk
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2025-05-08 14:02:35 -0400
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-05-12 12:33:07 +0100
commit8be0c0d6901669661fca578f474cd51cd284d35a (patch)
tree9a0caf26bfa3e6aa3d92064433186c8524f0a8bc /tests/composite_pk
parent42ab99309d347f617d60751c2e8d627fb2963049 (diff)
Fixed #36373 -- Fixed select_related() crash on foreign object for a composite pk.
Thanks Jacob Walls for the report and Sarah for the in-depth review.
Diffstat (limited to 'tests/composite_pk')
-rw-r--r--tests/composite_pk/models/tenant.py10
-rw-r--r--tests/composite_pk/tests.py8
2 files changed, 14 insertions, 4 deletions
diff --git a/tests/composite_pk/models/tenant.py b/tests/composite_pk/models/tenant.py
index c85869afa7..65eb0feae8 100644
--- a/tests/composite_pk/models/tenant.py
+++ b/tests/composite_pk/models/tenant.py
@@ -14,17 +14,18 @@ class Token(models.Model):
secret = models.CharField(max_length=10, default="", blank=True)
-class BaseModel(models.Model):
+class AbstractUser(models.Model):
pk = models.CompositePrimaryKey("tenant_id", "id")
tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE)
+ email = models.EmailField(unique=True)
id = models.SmallIntegerField(unique=True)
class Meta:
abstract = True
-class User(BaseModel):
- email = models.EmailField(unique=True)
+class User(AbstractUser):
+ pass
class Comment(models.Model):
@@ -35,13 +36,14 @@ class Comment(models.Model):
related_name="comments",
)
id = models.SmallIntegerField(unique=True, db_column="comment_id")
- user_id = models.SmallIntegerField()
+ user_id = models.SmallIntegerField(null=True)
user = models.ForeignObject(
User,
on_delete=models.CASCADE,
from_fields=("tenant_id", "user_id"),
to_fields=("tenant_id", "id"),
related_name="comments",
+ null=True,
)
text = models.TextField(default="", blank=True)
integer = models.IntegerField(default=0)
diff --git a/tests/composite_pk/tests.py b/tests/composite_pk/tests.py
index 5dea23c9f2..91cbee0635 100644
--- a/tests/composite_pk/tests.py
+++ b/tests/composite_pk/tests.py
@@ -184,6 +184,14 @@ class CompositePKTests(TestCase):
with self.assertNumQueries(1):
self.assertEqual(user.email, self.user.email)
+ def test_select_related(self):
+ Comment.objects.create(tenant=self.tenant, id=2)
+ with self.assertNumQueries(1):
+ comments = list(Comment.objects.select_related("user").order_by("pk"))
+ self.assertEqual(len(comments), 2)
+ self.assertEqual(comments[0].user, self.user)
+ self.assertIsNone(comments[1].user)
+
def test_model_forms(self):
fields = ["tenant", "id", "user_id", "text", "integer"]
self.assertEqual(list(CommentForm.base_fields), fields)