# Possible Bug: .values("pk") and .values("id") generate different SQL since 5.2

**URL:** <https://forum.djangoproject.com/t/possible-bug-values-pk-and-values-id-generate-different-sql-since-5-2/40469>\
**Category:** ORM\
**Created:** [April 17, 2025, 12:47pm UTC](https://forum.djangoproject.com/t/possible-bug-values-pk-and-values-id-generate-different-sql-since-5-2/40469 "2025-04-17T12:47:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![maqnius](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maqnius/32/11108_2.png) [@maqnius](https://forum.djangoproject.com/u/maqnius)\
**Post date:** [April 17, 2025, 12:47pm UTC](https://forum.djangoproject.com/t/possible-bug-values-pk-and-values-id-generate-different-sql-since-5-2/40469/1 "2025-04-17T12:47:12Z")

</div>

I encountered a bug in combination with the [django\_cte](https://github.com/dimagi/django-cte) package but I’m not entirely sure if it’s only a [django\_cte](https://github.com/dimagi/django-cte) bug and if the behaviour is really intended in Django 5.2.

Since Django 5.2, querying the primary keys using `.values("id")` or `.values("pk")` does not produce the same SQL anymore, since the queried values are aliased as “id” or “pk”.

Before 5.2:

```bash
In [1]: str(Location.objects.values("pk").query)
Out[1]: 'SELECT "location"."id" FROM "location"'

In [2]: str(Location.objects.values("id").query)
Out[2]: 'SELECT "location"."id" FROM "location"'

```

Since 5.2:

```bash
In [1]: str(Location.objects.values("pk").query)
Out[1]: 'SELECT "location"."id" AS "pk" FROM "location"'

In [2]: str(Location.objects.values("id").query)
Out[2]: 'SELECT "location"."id" AS "id" FROM "location"'

```

It seems like a quite fundamental difference, but I cannot produce a failing query using Django Querysets only.

[django\_cte](https://github.com/dimagi/django-cte) stumbles here because they have indirectly hardcoded to replace `.pk` with `.id` at [some point](https://github.com/dimagi/django-cte/blob/main/django_cte/meta.py#L61) when constructing joints. Thereby it can happen that in a resulting SQL a join on `id` fails because the `id` field is aliased as `pk`.

---

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [April 17, 2025, 1:37pm UTC](https://forum.djangoproject.com/t/possible-bug-values-pk-and-values-id-generate-different-sql-since-5-2/40469/2 "2025-04-17T13:37:42Z")

</div>

See this topic just below yours called [Selecting primary key in 5.2](https://forum.djangoproject.com/t/selecting-primary-key-in-5-2/40215) and respective [django-cte PR](https://github.com/dimagi/django-cte/pull/108) thanks to @simwr872.

---

<div class="post-metadata">

**Author:** ![maqnius](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/maqnius/32/11108_2.png) [@maqnius](https://forum.djangoproject.com/u/maqnius)\
**Post date:** [April 17, 2025, 1:56pm UTC](https://forum.djangoproject.com/t/possible-bug-values-pk-and-values-id-generate-different-sql-since-5-2/40469/3 "2025-04-17T13:56:45Z")

</div>

Oh thanks, haven’t found that topic using the search. Thanks!
