I have two models (just for example):
class One(BaseModel):
name = models.CharField()
twos = models.ManyToManyField(Two)
class Two(BaseModel):
title = models.CharField()
When I try to add a list of ids
of model Two
to model One
with one.twos.add(*[id1, id2])
it works until I pass a wrong id
, when this fails with
psycopg2.errors.ForeignKeyViolation: insert or update on table "one_twos" violates foreign key constraint "one_two_id_572a5216_fk_twos"
DETAIL: Key (two_id)=(e2871924-5bb4-492e-b7c3-4c5ca3cc7f5e) is not present in table "twos_two".
django.db.utils.IntegrityError: insert or update on table "one_twos" violates foreign key constraint "one_two_id_572a5216_fk_twos"
DETAIL: Key (two_id)=(e2871924-5bb4-492e-b7c3-4c5ca3cc7f5e) is not present in table "twos_two".
This does not seem to be the race condition (mentioned here Django: IntegrityError during Many To Many add()).
I need to tell the front-end that such-and-such id
is not valid, but I can't catch these two IntergityErrors
to re-raise my custom exception with a message and id
.
Would highly appreciate any help with this.