django-q: [Error] select_for_update cannot be used outside of a transaction.

This Django error (raised from SQL Compiler) pops up in my logs and prevent any scheduled tasks to run. The error is raised from this django_q line, which is very strange since the whole try block is within the transaction.atomic() context manager.

Any idea on why this is happening and how to fix it? Thanks!

Config:

  • db: Postgres 11 with psycopg2 interface
  • django-q 1.2.1
  • django 3.0
  • python 3.8

Edit

The error is reproduced in this basic demo app

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 20 (20 by maintainers)

Most upvoted comments

So I guess what’s happening here is that the atomic transaction is started on the wrong database connection. You can specify the database with the using option. I’ll investigate a bit further.

Fixed in release 1.2.2, tested and approved!

Thanks @maerteijn and @Koed00 💯

I will do it this Friday, I didn’t forget 🙂

That’s cool! I’ll review once again and check for the implications of these changes and will add a PR when ready. Have a nice weekend!

Sqlite does not do select_for_update locks so that’s why. I’ll check your repo in the following days to see if I can find what’s going on.

Did you configure the ORM settings to use the correct database connection (in your case: admin)?

Q_CLUSTER = {
    'name': 'DjangORM',
    'workers': 4,
    ...
    'orm': 'default'  <---
}