# Why does .exists() respect .select\_for\_update() while .count() doesn't?

**URL:** <https://forum.djangoproject.com/t/why-does-exists-respect-select-for-update-while-count-doesnt/11114>\
**Category:** Using the ORM\
**Created:** [December 20, 2021, 3:43pm UTC](https://forum.djangoproject.com/t/why-does-exists-respect-select-for-update-while-count-doesnt/11114 "2021-12-20T15:43:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![akazecik](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@akazecik](https://forum.djangoproject.com/u/akazecik)\
**Post date:** [December 20, 2021, 3:43pm UTC](https://forum.djangoproject.com/t/why-does-exists-respect-select-for-update-while-count-doesnt/11114/1 "2021-12-20T15:43:37Z")

</div>

Hello, I’ve just noticed that when I do

```auto
MyModel.objects.select_for_update().count() != 0

```

it runs successfully. However, when I do

```auto
MyModel.objects.select_for_update().exists()

```

I get an error `select_for_update cannot be used outside of a transaction`.

I checked the queries that django made and they are as follows:

```auto
SELECT COUNT(*) AS "__count" FROM "example_mymodel" # .count()
SELECT (1) AS "a" FROM "example_mymodel" LIMIT 1 FOR UPDATE # .exists()

```

I tested it by creating a completely new app in my existing django project (`django==3.2.10` + PostgreSQL 12.0.1) and this is the model I used:

```auto
class MyModel(models.Model):
    my_field = models.CharField(max_length=256)

```

However, when I created a completely new django project (`django==3.2.10` + SQLite 3.31.1) with the same app and model, the `.exists()` query passed without wrapping it with `transaction.atomic()`. This is the query in the second test

```auto
SELECT (1) AS "a" FROM "example_mymodel" LIMIT 1

```

Note that it doesn’t contain `FOR UPDATE`.

All tests were done via `./manage.py shell` and looked up with `django.connection.queries`.

Is this expected? Why is there such discrepancy between `.count()` and `.exists()` or different databases?

---

<div class="post-metadata">

**Author:** ![szuliq](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/szuliq/32/7664_2.png) [@szuliq](https://forum.djangoproject.com/u/szuliq)\
**Post date:** [March 2, 2022, 10:11am UTC](https://forum.djangoproject.com/t/why-does-exists-respect-select-for-update-while-count-doesnt/11114/2 "2022-03-02T10:11:58Z")

</div>

`.exists()` and SQLite work as documented here [QuerySet API reference | Django documentation | Django](https://docs.djangoproject.com/en/3.2/ref/models/querysets/#select-for-update)

> Evaluating a queryset with `select_for_update()` in autocommit mode on backends which support `SELECT ... FOR UPDATE` is a [`TransactionManagementError`](https://docs.djangoproject.com/en/3.2/ref/exceptions/#django.db.transaction.TransactionManagementError) error because the rows are not locked in that case.

and

> Using `select_for_update()` on backends which do not support `SELECT ... FOR UPDATE` (such as SQLite) will have no effect.

As for the different behavior of `.count()` I’m not really sure.
