开发者

Django: Mixed managed and raw db commits - TransactionManagementError

开发者 https://www.devze.com 2023-01-27 11:53 出处:网络
I\'m writing a bulk insert script using Django\'s ORM + custom raw SQL. The code has the following outline:

I'm writing a bulk insert script using Django's ORM + custom raw SQL. The code has the following outline:

import sys, os
f开发者_JAVA百科rom django.core.management import setup_environ
from my_project import settings
from my_project.my_app.models import Model1, Model2
setup_environ(settings)
from django.db import transaction
from django.db import connection

@transaction.commit_manually
def process_file(relevant_file):

    data_file = open(relevant_file,'r')

    cursor = connection.cursor()

    while 1:
        line = data_file.readline()
        if line == '':
            break

        if not(input_row_i%1000):
            transaction.commit()

        if ([some rare condition]):
            model_1 = Model1([Some assignments based on line])
            model_1.save()

        values = [Some values based on line]
        cursor.execute("INSERT INTO `table_1` ('field_1', 'field_2', 'field_3') VALUES (%i, %f, %s)", values)

    data_file.close()
    transaction.commit()

I keep getting the following error:

django.db.transaction.TransactionManagementError: Transaction managed block ended with pending COMMIT/ROLLBACK

How can I solve this?


Use transaction.commit_unless_managed()

I've written a post to explain in greater detail with an example.


I started getting this exception in a similar circumstance. The django ORM was actually throwing a django.core.exceptions.ValidationError error because a date was incorrectly formatted. Because I was using manual transaction processing to batch database writes, the Django transactions processing code was trying to cleanup inside the raised django.core.exceptions.ValidationError exception and threw it's own exception of django.db.transaction.TransactionManagementError. Try a try / except around your model_1 code to see if any other exceptions are being thrown. Something like:

try:
    model_1 ...
    model_1.save()
except:
    print "Unexpected error:", sys.exc_info()[0]
    print 'line:', line

to see if there are any problems with the input data to object creation code.


You could try a workaround - place a transaction.commit() right after the model_1.save(). I think you need to isolate raw and ORM transactions.

0

精彩评论

暂无评论...
验证码 换一张
取 消