Wednesday, June 24, 2015

Re: Atomic block termination question

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCAAGBQJVitweAAoJEC0ft5FqUuEh+KAQAKCCBFLaoNXfkH+zzDVq7XOt
Y9iFMoaL0rfAL22ErDj334CCYwNQAPsmHSp31DricofVs8np+iDy+n86gfSekZpa
agNGfdNCRpn9Hlftji4MF8G3a8LbYHLtl6Jfh4YlrzyMyR/C+R833eossS8ZNLN5
45wX+6QwhiP/SwPc54Atj0V+Acu/y7jihNvtwbKYqM5qPqLanDCx4yMveXSLIZDH
SsBKfxqOJvyy2CIkyOGH8O1x34y3sFZJFlVs4K1yZD5OBsPE5883UL9XHsNnnP23
RrNhnEYjKGlOep98mNk3f/XBlJUIVESXf+tRpx2HG8oNT2YCptxs7uhDhE5EpnjX
jdPeSdvqJH01hIIxYuoisGalWJIDka/YtHVhYe1xyv7UpbUGH4vLKV18U4+4FpE3
M0NSY6JrYM/vyjx3T47NZX6HcH/6alN8LS3FFJtZtf+jyWu4fJqmAN8DNXgY/4fi
inH3zmK8JoXvw+sKQ0/QdPXPm4NAXcZrpAx3bDcAzaCW8hIltJdekICLdIC0QU3k
iMXCNN48geNnQrGD/gMTA7Zs65nIoH3utNqiYQ8UqsfJ8w/uXfF38adSIbKXilIT
NwGUnx/zloFVGssf8rSIAFF99SJUCJEW3bvPdhYaj4RuIvDG7eOSl6q/hiW22JM9
tp9IbOfxp9KxtjPzC5YQ
=ngrB
-----END PGP SIGNATURE-----
Hi Mike,

On 06/24/2015 02:16 AM, Mike Dewhirst wrote:
> On 24/06/2015 4:43 PM, Mike Dewhirst wrote:
>> When saving a model I'm getting a TransactionManagementError - You can't
>> execute queries until the end of the 'atomic' block
>>
>> Ticket #21540 seems fairly explicit at least where Postgres is
>> concerned. TransactionManagementError prevents what I want to do and I'm
>> not a nuclear expert.
>>
>> How do I terminate the save() method code in that atomic block and then
>> immediately execute my queries?

I'm afraid this description of what you're trying to do is too vague to
be useful. Maybe some sample code would help?

TransactionManagementError is a symptom, not a cause. It means that a
database error occurred inside a transaction, which leaves the
transaction in an unpredictable state, so Postgres wants you to roll
back the transaction (or roll back to a savepoint prior to the error)
before issuing any more database queries.

Possible solutions include:

a) Figuring out why there was a database error, and fixing it so it
doesn't occur.

b) Wrapping the code that might cause a database error in its own
`transaction.atomic` block, so on error that bit of code is rolled back
and later queries within the same transaction can go forward.

> I have implemented a workaround but not sure if it is the best way. Any
> comment appreciated ...
>
> In the model's clean() method I test for self.pk and if true, execute
> the queries which previously caused the problem. This seems to work but
> hasn't had any testing in production.

Again it's hard to tell without seeing code or traceback, but it sounds
like probably what you've done here is fix the condition that was
causing the error in the first place (that is, solution (a) above). It
sounds like you were probably trying to do some related-model queries
with an unsaved model, and now you've guarded those queries to only
occur if the model is saved. If so, that's not a workaround, it's the
best solution.

>> I need the save() to complete so I can get_or_create some 1:1 records
>> belonging to the model being saved.

Again, sample code would really illuminate what you're trying to do.

Carl

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/558ADC1E.6070709%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment