0

I have a Django application and I'm using postgres. I try to execute the bollowing line in one of my tests:

print BillingUser.objects.all()

And I get the following error:

"current transaction is aborted, commands ignored until end of transaction block."

My postresql log:

ERROR:  duplicate key value violates unique constraint "billing_rental_wallet_id_key"
STATEMENT:  INSERT INTO "billing_rental" ("wallet_id", "item_id", "end_time", "time", "value", "index", "info") VALUES (61, 230, E'2010-02-11 11:01:01.092336', E'2010-02-01 11:01:01.092336', 10.0, 1, NULL)
ERROR:  current transaction is aborted, commands ignored until end of transaction block
STATEMENT:  INSERT INTO "billing_timeable" ("creation_date", "update_date") VALUES (E'2010-02-01 11:01:01.093504', E'2010-02-01 11:01:01.093531')
ERROR:  current transaction is aborted, commands ignored until end of transaction block
STATEMENT:  SELECT "billing_timeable"."id", "billing_timeable"."creation_date", "billing_timeable"."update_date", "billing_billinguser"."timeable_ptr_id", "billing_billinguser"."username", "billing_billinguser"."pin", "billing_billinguser"."sbox_id", "billing_billinguser"."parental_code", "billing_billinguser"."active" FROM "billing_billinguser" INNER JOIN "billing_timeable" ON ("billing_billinguser"."timeable_ptr_id" = "billing_timeable"."id") LIMIT 21

How can I fix that?

Thanks, Arshavski Alexander.

3
  • Are you sure your not trying to insert somewhere above this print line? Have you overwritten any methods in the BillingUser class? More code would be very helpful. Commented Feb 1, 2010 at 9:27
  • My tests.py is here: slexy.org/view/s21qJe144O my models.py is here: slexy.org/view/s21EaSv1yu Commented Feb 1, 2010 at 9:35
  • In which line is this print command? Commented Feb 1, 2010 at 10:24

3 Answers 3

1

Ok... looking at the PostgreSQL log, it does look that you are doing a wrong insert that will abort the transaction... now, looking at your code I think the problems lies here:

at lines 78-81

    currency = Currency.objects.all()[2]
    if not Wallet.objects.filter(user=user):
        wallet = Wallet(user=user, currency=currency)
        wallet.save()

You will create a wallet for the current user, but then on line 87-88 you wrote:

    user.wallet.amount = 12.0
    user.wallet.save()

However, as you save the wallet after retrieving the user, it does not know that you had already created a wallet for him, and having a OneToOne relationship, this will cause the error you're having... I think what you should do is to add a line after 81:

    currency = Currency.objects.all()[2]
    if not Wallet.objects.filter(user=user):
        wallet = Wallet(user=user, currency=currency)
        wallet.save()
        user.wallet = wallet

That should solve the issue....

Sign up to request clarification or add additional context in comments.

Comments

0

You insert data in some of your test functions. After invalid insert DB connections is in fail state. You need to rollback transaction or turn it off completely. See django docs on transactions and testing them.

Comments

0

From the log it looks like you are trying to insert an item with a duplicate ID which throws an error and the rest of your code can't access the DB anymore. Fix that query, and it should work.

2 Comments

The insert must be happening somewhere before the printing line. Possibly quite far.
My tests.py is here: slexy.org/view/s21qJe144O my models.py is here: slexy.org/view/s21EaSv1yu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.