Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transaction.atomic() problem with DATABASE_ROUTERS #182

Open
asr-systify opened this issue Jun 6, 2023 · 2 comments
Open

transaction.atomic() problem with DATABASE_ROUTERS #182

asr-systify opened this issue Jun 6, 2023 · 2 comments

Comments

@asr-systify
Copy link

Hi,

I use this library in a project with DATABASE_ROUTERS.
As soon as django_apscheduler is not in the 'default' database, I get the following error message:
"select_for_update cannot be used outside of a transaction".

with transaction.atomic():

Which in my opinion is also conclusive, because atomic points to the 'default' db without parameters.
The following sample change fixes my problem:

DATABASE_APPS_MAPPING = {
'django_apscheduler': 'test',
}

with transaction.atomic(using='test'):

Is there another approach to this problem case? I couldn't find any other solution when django_apscheduler is not in my 'default' db.

version:
Django==3.2.9
django-apscheduler==0.6.2

@rizkyarlin
Copy link

+1
I also ran into same problem. But I can confirm that django_apscheduler is in my default DB.

@icewisb
Copy link

icewisb commented Apr 8, 2024

@asr-systify
I have a clumsy method, without modifying the source code.

## router.py
class DBRouter(object):
    def db_for_read(self, model, **hints):
        return settings.DB_MAPPING.get(model._meta.app_label, "another_default")

    def db_for_write(self, model, **hints):
        return settings.DB_MAPPING.get(model._meta.app_label, "another_default")

## settings.py
DB_MAPPING = {
    "app_label1": "db_alias1",
     ...
}

DATABASES = {
    "default": {},  # which that "apscheduler" is using
    "another_default": {},  # rename "old" default to "another_default"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants