Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
94 views
in Technique[技术] by (71.8m points)

How to add manually on delete SQL constraints in Django?

In an existing Django project I experience critical performance issues when objects of a certain model are deleted. Actually the whole infrastructure breaks down.

I'm pretty that this is caused by a foreign key field to another model with many and large entries.

After several hours of googling I found a possible solution: Implementing on delete constraints on database level.

I also found a long lasting discussion in a Django PR https://github.com/django/django/pull/8661 which would enable this feature. But I seems like it never reached 100% and will not be merged in the near future.

Another way to solve my issue is mentioned in Django Docs https://docs.djangoproject.com/en/3.1/ref/models/fields/#django.db.models.DO_NOTHING

DO_NOTHING

Take no action. If your database backend enforces referential integrity, this will cause an IntegrityError unless you manually add an SQL ON DELETE constraint to the database field.

I'm using MariaDB. And I want to add a SET_NULL constraint to only one foreign key field of one model on database level.

How can I do that in Django? I can perfectly live with hard coded ugly solutions, if they solve my problem and don't introduce new ones.

question from:https://stackoverflow.com/questions/65600464/how-to-add-manually-on-delete-sql-constraints-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...