Opened 20 months ago

Last modified 20 months ago

#34523 closed Bug

update_or_create not work in parallel insertion — at Initial Version

Reported by: gatello-s Owned by: nobody
Component: Database layer (models, ORM) Version: 4.2
Severity: Normal Keywords: update_or_create TransactionManagementError
Cc: Anton Plotkin, Francesco Panico Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am tests on mariadb 10.5.19 (myisam).
This test work fine on django-3.2.16

class TransactionManagementErrorTest(TestCase):

    class TestModel(models.Model):
        field = models.IntegerField(null=True)

        class Meta(object):
            db_table = 'test_model_update_or_create'

        class QuerySet(models.QuerySet):
            def create(self, **kwargs):
                super().create(**kwargs)  # simulate parallel insertion
                return super().create(**kwargs)
    
        class TestModelManager(models.Manager.from_queryset(QuerySet)):
            pass

        objects = TestModelManager()

    def exec_sql(self, sql):
        from django.db import connections, router
        db = router.db_for_write(self.TestModel)
        connection = connections[db]
        connection.cursor().execute(sql)

    def setUp(self):
        super().setUp()
        self.exec_sql(
            'CREATE TABLE IF NOT EXISTS `test_model_update_or_create` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `field` integer NULL);'
        )

    def tearDown(self):
        self.exec_sql(
            'DROP TABLE IF EXISTS `test_model_update_or_create`;'
        )
        super().tearDown()

    def test_update_or_create(self):
        self.TestModel.objects.update_or_create(id=1, defaults={'field': 2})

Change History (0)

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