Why is the “on_update” option not present in Django Relationship fields?

半腔热情 提交于 2021-01-28 19:27:42

问题


I'm using Django 3.0 + MariaDB. I've created a models.py from an existent database with the "python manage.py inspectdb > models.py" command. I need now to set the options for the Foreign Keys. As you know in a database you can have multiple options for the Foreign Keys: the first classic is "ON_DELETE" and the second is 'ON_UPDATE' (You can even have more options in PostgreSQL). In Django, there is just an "on_delete", but no "on_update" option, what I found very surprising. I found nothing on the official documentation about this. Neither on some old posts with the same question, the responses were not conclusive and focused on the "on_delete". I'm asking about the "ON_UPDATE' option, nothing else. So, where is this option, or why is it not present in Django's ORM ?


回答1:


Django does not actually use database cascade options even for on_delete

on_delete doesn’t create an SQL constraint in the database. Support for database-level cascade options may be implemented later.

There are convenient advantages of the following as your app can react to pre_delete and post_delete signals, but there is also slight disadvantage in terms of performance


Regarding ON_UPDATE

It was not implemented and it was fairly rarely used as normally you would never change your model id's

You still can create SQL migration if you need something specific on database level or emulate it on application level for better control (for instance overriding model save method)


Regarding additional database constraints not related to foreign key, there are docs related to them here and specific to postgreSQL here



来源:https://stackoverflow.com/questions/61648319/why-is-the-on-update-option-not-present-in-django-relationship-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!