Consider this Django code:
class User(models.Model):
name = models.CharField(null=True, blank=False, verbose_name=_("Name"), help_text='User Name', max_length=256)
class UsersGroup(models.Model):
name = models.CharField(null=False, blank=False, verbose_name=_("Name"), help_text='Users Group Name', max_length=256)
users = models.ManyToManyField(User)
# ...
with transaction.atomic():
group.users.add(user)
What if the user was deleted from the DB before the transaction starts? It would add an nonexistent user to group.users
. This is an error.
What to do in this situation to preserve DB integrity?