Can't execute queries until end of atomic block in my data migration on django 1.7

后端 未结 3 493
别那么骄傲
别那么骄傲 2021-02-07 12:42

I have a pretty long data migration that I\'m doing to correct an earlier bad migration where some rows were created incorrectly. I\'m trying to assign values to a new column b

相关标签:
3条回答
  • 2021-02-07 12:58

    This is similar to the example in the docs.

    First, add the required import if you don't have it already.

    from django.db import transaction
    

    Then wrap the code that might raise an integrity error in an atomic block.

    try:
        with transaction.atomic():
            inst.new_col = new_col_mapping[c]
            inst.save()
    except IntegrityError:
        inst.delete()
    

    The reason for the error is explained in the warning block 'Avoid catching exceptions inside atomic!' in the docs. Once Django encounters a database error, it will roll back the atomic block. Attempting any more database queries will cause a TransactionManagementError, which you are seeing. By wrapping the code in an atomic block, only that code will be rolled back, and you can execute queries outside of the block.

    0 讨论(0)
  • 2021-02-07 13:08

    Each migration is wrapped around one transaction, so when something fails during migration, all operations will be cancelled. Because of that, each transaction in which something failed, can't take new queries (they will be cancelled anyway).

    Wrapping some operations with with transaction.atomic(): is not good solution, because you won't be able to cancel that operation when something will fail. Instead of that, avoid integrity errors by doing some more checks before saving data.

    0 讨论(0)
  • 2021-02-07 13:23

    It seems that the same exception can have a variety of causes. In my case it was caused by an invalid model field name: I used a greek letter delta

    0 讨论(0)
提交回复
热议问题