mirror of
https://github.com/django/django.git
synced 2025-05-16 20:06:29 +00:00
Fixed #36326 -- Added CompositePrimaryKey support in QuerySet.raw().
Signed-off-by: SaJH <wogur981208@gmail.com>
This commit is contained in:
parent
d755a98b84
commit
1831f7733d
@ -165,7 +165,9 @@ class RawModelIterable(BaseIterable):
|
||||
annotation_fields,
|
||||
) = self.queryset.resolve_model_init_order()
|
||||
model_cls = self.queryset.model
|
||||
if model_cls._meta.pk.attname not in model_init_names:
|
||||
if any(
|
||||
f.attname not in model_init_names for f in model_cls._meta.pk_fields
|
||||
):
|
||||
raise exceptions.FieldDoesNotExist(
|
||||
"Raw query must include the primary key"
|
||||
)
|
||||
|
@ -201,6 +201,9 @@ Models
|
||||
:ref:`a forced update <ref-models-force-insert>` results in no affected rows,
|
||||
instead of a generic :exc:`django.db.DatabaseError`.
|
||||
|
||||
* :meth:`.QuerySet.raw` now supports models with a
|
||||
:class:`~django.db.models.CompositePrimaryKey`.
|
||||
|
||||
Pagination
|
||||
~~~~~~~~~~
|
||||
|
||||
|
@ -11,7 +11,7 @@ except ImportError:
|
||||
|
||||
from django import forms
|
||||
from django.core import serializers
|
||||
from django.core.exceptions import FieldError
|
||||
from django.core.exceptions import FieldDoesNotExist, FieldError
|
||||
from django.db import IntegrityError, connection
|
||||
from django.db.models import CompositePrimaryKey
|
||||
from django.forms import modelform_factory
|
||||
@ -158,6 +158,20 @@ class CompositePKTests(TestCase):
|
||||
users = User.objects.values_list("pk").order_by("pk")
|
||||
self.assertNotIn('AS "pk"', str(users.query))
|
||||
|
||||
def test_raw(self):
|
||||
users = User.objects.raw("SELECT * FROM composite_pk_user")
|
||||
self.assertEqual(len(users), 1)
|
||||
user = users[0]
|
||||
self.assertEqual(user.tenant_id, self.user.tenant_id)
|
||||
self.assertEqual(user.id, self.user.id)
|
||||
self.assertEqual(user.email, self.user.email)
|
||||
|
||||
def test_raw_missing_PK_fields(self):
|
||||
query = "SELECT tenant_id, email FROM composite_pk_user"
|
||||
msg = "Raw query must include the primary key"
|
||||
with self.assertRaisesMessage(FieldDoesNotExist, msg):
|
||||
list(User.objects.raw(query))
|
||||
|
||||
def test_only(self):
|
||||
users = User.objects.only("pk")
|
||||
self.assertSequenceEqual(users, (self.user,))
|
||||
|
Loading…
x
Reference in New Issue
Block a user