Hi, I am trying to delete an Account that delete in cascade others objects, but I have a post_delete in Car model that create a Log that have relation with this Account.
-- The django apparently can not handle the error. How to solve this?
Models:
Error:
class Account(models.Model):
pass
class Car(models.Model):
account = models.ForeignKey('Account', on_delete=models.CASCADE)
class Log(models.Model):
account = models.ForeignKey('Account', on_delete=models.CASCADE)
class CarLog(Log):
car = models.ForeignKey('Car', null=True, blank=True, on_delete=models.SET_NULL)
@receiver(post_delete, sender=Car):
def create_car_log(sender, instance, **kwargs):
try:
CarLog.objects.create(
account=instance.account,
)
except:
pass
Shell:
>>> account = Account.objects.create()
>>> car = Car.objects.create(account=account)
>>> CarLog.objects.create(account=account, car=car)
>>> account.delete() # when delete car will try to create a log related with that account, but...insert or update on table "myapp_log" violates foreign key constraint "myapp_log_account_id_It's happening this error rather than the exception.6ea8d7a6_fk_myapp_account_id" DETAIL: Key (account_id)=(11) is not present in table "myapp_account".
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/30775c1b-e7e8-439d-9a6e-7cef8667c6ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment