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
>>> 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_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.
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.
--
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.
INSTALLED_APPS = [
'core', # here: Account, Log
'myapp', # here: Car, CarLog
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/02b1ab5b-d7e3-4df2-9738-9c1636e931c5%40googlegroups.com.
SELECT * FROM "myapp_car" WHERE "myapp_car"."account_id" IN (1);
SELECT * FROM "myapp_carlog" INNER JOIN "myapp_log" ON ("myapp_carlog"."log_ptr_id" = "myapp_log"."id") WHERE "myapp_carlog"."car_id" IN (1);
SELECT * FROM "myapp_log" WHERE "myapp_log"."account_id" IN (1);
-- Django doing the m2m DELETEs and UPDATEs
DELETE FROM "myapp_carlog" WHERE "myapp_carlog"."log_ptr_id" IN (1);
UPDATE "myapp_carlog" SET "car_id" = NULL WHERE "myapp_carlog"."log_ptr_id" IN (1);
DELETE FROM "myapp_car" WHERE "myapp_car"."id" IN (1);
-- This is the signal firing for Car delete
SAVEPOINT "s140735184359424_x3";
SELECT "myapp_account"."id" FROM "myapp_account" WHERE "myapp_account"."id" = 1;
INSERT INTO "myapp_log" ("account_id") VALUES (1) RETURNING "myapp_log"."id";
INSERT INTO "myapp_carlog" ("log_ptr_id", "car_id") VALUES (4, NULL);
RELEASE SAVEPOINT "s140735184359424_x3";
-- Outside the signal, more cascades
DELETE FROM "myapp_log" WHERE "myapp_log"."id" IN (1);
-- Finally, delete the account, where we blow up
DELETE FROM "myapp_account" WHERE "myapp_account"."id" IN (1);
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9b6ded86-c084-4842-ab41-4f40624df0a6%40googlegroups.com.