Ahh, Postgres is the problem. When your exception is thrown then Postgres
aborts the rest of the transaction. That's how its transaction handling
works. Even though you ignore the exception in the myapp code, it will
still cause the transaction to abort when Django tries to call commit().
When I was testing I was using sqlite, which behaves differently.

See the note here:
https://docs.djangoproject.com/en/1.9/topics/db/transactions/#handling-exceptions-within-postgresql-transactions

This works for me:

@receiver(post_delete, sender=Car)
def create_car_log(sender, instance, **kwargs):
    sid = transaction.savepoint()
    try:
        CarLog.objects.create(
            account=instance.account,
        )
        transaction.savepoint_commit(sid)
    except:
        transaction.savepoint_rollback(sid)

What I don't get is that using a "with transaction.atomic()" inside the try
block should do the same thing. But it's not. Maybe someone else knows?

On Sat, Apr 23, 2016 at 10:19 PM, Neto <paulosouzamac...@gmail.com> wrote:

> Stephen, I am using Django 1.9.5, PostgreSQL 9.3
> I do not know, maybe the order of the apps may be interfering in the way
> Django sorts the commands to be sent to the postgresql.
>
> INSTALLED_APPS = [
>     'core',  # here: Account, Log
>     'myapp',  # here: Car, CarLog
>
>
> What is happening is that post_delete is trying to create a log for an
> account that does not exist. The exception doesn't works.
> This seems to be a bug.
>
> Em sábado, 23 de abril de 2016 18:28:08 UTC-3, Stephen Butler escreveu:
>>
>> Sorry, I did miss that.
>>
>> I created a quick test project in 1.9 and ran your sample. It works fine
>> for me. The delete() returns that it deleted 4 objects: the Account, Car,
>> Log, and CarLog. There's something else in your project that is causing the
>> error.
>>
>> On Sat, Apr 23, 2016 at 3:42 PM, Neto <paulosou...@gmail.com> wrote:
>>
>>> Stephen, CarLog is inheriting Log.
>>>
>>>
>>> Em sábado, 23 de abril de 2016 17:14:57 UTC-3, Stephen Butler escreveu:
>>>>
>>>> Look a little closer at the error message:
>>>>
>>>> Error:
>>>>>
>>>>> insert or update on table "myapp_log" violates foreign key constraint 
>>>>> "myapp_log_account_id_6ea8d7a6_fk_myapp_account_id"
>>>>> DETAIL:  Key (account_id)=(11) is not present in table "myapp_account".
>>>>>
>>>>> It's happening this error rather than the exception.
>>>>>
>>>>
>>>> The table is myapp_log, not myapp_carlog. The error isn't in the
>>>> post_delete signal you're showing up. Do you have a post_delete for Account
>>>> objects?
>>>>
>>> --
>>> 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...@googlegroups.com.
>>> To post to this group, send email to django...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/92e72312-5678-428b-ad7f-360ad911ceaa%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/92e72312-5678-428b-ad7f-360ad911ceaa%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/02b1ab5b-d7e3-4df2-9738-9c1636e931c5%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/02b1ab5b-d7e3-4df2-9738-9c1636e931c5%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAD4ANxW%3DLn%3DKYayVnK%2B2tmpNVtSTS3Jicv9MEwUFyQnGUEzGEw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to