[Answered ]-How do I manage deletion with multiple foreign keys to the same table in Django?

1👍

You have such options:

  • A routine. It can be made as a Command and something like crontab. Or it could be designed as a periodiq routine. This way you can repeatedly select all Thing models which have no relations with ThingRelation.
  • signals.py action. This way when entry of ThingRelation is deleted, you should check both the first_thing and second_thing in order to know whether the have any more ThingRelation relations pointing to them.
  • DB trigger (e.g. for PostgreSQL). The same idea as signal.py solution, but on DB level.

Which one should you choose? Depends on details of your exact objective. As for me, I use periodiq option on simple cases and DB trigger if I aim on high performance.

Leave a comment