Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33064 closed Bug (worksforme)

Created Model is not showing in Django Atomic Trasaction Block

Reported by: Dipen Sompura Owned by: nobody
Component: Database layer (models, ORM) Version: 3.1
Severity: Normal Keywords: transaction atomic mysql
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Following code not working, it should increase count within the block itself:

I am using django with mysql database.

>>> len(ModelObject.objects.all())
89
>>> with transaction.atomic():
...     ModelObject.objects.create(modelId="123")
...     print(len(ModelObject.objects.all()))
... 
<ModelObject: ModelObject object (16125)>
89
>>> len(ModelObject.objects.all())
90

Change History (2)

comment:1 by Mariusz Felisiak, 3 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: worksforme
Status: newclosed

Thanks for the report, however I cannot reproduce this issue on MySQL (8.0.26, 5.6.50), SQLite, or PostgreSQL.

Please reopen the ticket if you can debug your issue and provide details about why and where Django is at fault.

in reply to:  1 comment:2 by Dipen Sompura, 3 years ago

Replying to Mariusz Felisiak:

Thanks for the report, however I cannot reproduce this issue on MySQL (8.0.26, 5.6.50), SQLite, or PostgreSQL.

Please reopen the ticket if you can debug your issue and provide details about why and where Django is at fault.

Thank you for looking into this. I am able to debug the issue. We are using reader and writer db instance with following DB router:

class DBRouter:

    def db_for_read(self, model, **hints):
        """Return read replica."""
        return 'replica1'

    def db_for_write(self, model, **hints):
        # Always return the default database
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        return True

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        return True

Any suggestion, where we can use same writer instance for the atomic block by keeping my DBRouter configuration as is?

Note: See TracTickets for help on using tickets.
Back to Top