From 8567df1da81402ed76b92b5a4a92090936a723ce Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Thu, 23 Oct 2014 12:06:58 -0400 Subject: [PATCH] Don't use Django's get_or_create in migrations. http://south.aeracode.org/ticket/325 https://stackoverflow.com/questions/20039250/django-1-6-transactionmanagementerror-database-doesnt-behave-properly-when-aut --- .../documents/south_migrations/0022_set_doc_type.py | 11 ++++++++++- .../sources/south_migrations/0007_set_doc_type.py | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/mayan/apps/documents/south_migrations/0022_set_doc_type.py b/mayan/apps/documents/south_migrations/0022_set_doc_type.py index 26e0188f4b..b3a4f486be 100644 --- a/mayan/apps/documents/south_migrations/0022_set_doc_type.py +++ b/mayan/apps/documents/south_migrations/0022_set_doc_type.py @@ -5,6 +5,15 @@ from south.v2 import DataMigration from django.db import models +def fake_get_or_create(model, *args, **kwargs): + try: + obj = model.objects.get(**kwargs) + except model.DoesNotExist: + obj = model(**kwargs) + obj.save() + return obj + + class Migration(DataMigration): def forwards(self, orm): @@ -12,7 +21,7 @@ class Migration(DataMigration): # Note: Don't use "from appname.models import ModelName". # Use orm.ModelName to refer to models in this application, # and orm['appname.ModelName'] for models in other applications. - orphan_doc_type, created = orm.DocumentType.objects.get_or_create(name='_orphan_document_') + orphan_doc_type = fake_get_or_create(orm.DocumentType, name='_orphan_document_') orm.Document.objects.filter(document_type__isnull=True).update(document_type=orphan_doc_type) def backwards(self, orm): diff --git a/mayan/apps/sources/south_migrations/0007_set_doc_type.py b/mayan/apps/sources/south_migrations/0007_set_doc_type.py index 5ef26230a5..a75f6a4606 100644 --- a/mayan/apps/sources/south_migrations/0007_set_doc_type.py +++ b/mayan/apps/sources/south_migrations/0007_set_doc_type.py @@ -5,6 +5,15 @@ from south.v2 import DataMigration from django.db import models +def fake_get_or_create(model, *args, **kwargs): + try: + obj = model.objects.get(**kwargs) + except model.DoesNotExist: + obj = model(**kwargs) + obj.save() + return obj + + class Migration(DataMigration): def forwards(self, orm): @@ -12,7 +21,7 @@ class Migration(DataMigration): # Note: Don't use "from appname.models import ModelName". # Use orm.ModelName to refer to models in this application, # and orm['appname.ModelName'] for models in other applications. - orphan_doc_type, created = orm['documents.DocumentType'].objects.get_or_create(name='_orphan_document_') + orphan_doc_type = fake_get_or_create(orm['documents.DocumentType'], name='_orphan_document_') orm.IntervalBaseModel.objects.filter(document_type__isnull=True).update(document_type=orphan_doc_type) def backwards(self, orm):