为什么要"on_update"?Django Relationships字段中不存在选项? [英] Why is the "on_update" option not present in Django Relationship fields?

查看:60
本文介绍了为什么要"on_update"?Django Relationships字段中不存在选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 3.0 + MariaDB.我已经使用"python manage.py inspectdb> models.py"命令从现有数据库中创建了一个models.py.现在,我需要为外键设置选项.如您所知,在数据库中,外键可以有多个选项:第一个经典是"ON_DELETE",第二个是"ON_UPDATE"(在PostgreSQL中甚至可以有更多选项).在Django中,只有一个"on_delete",但没有"on_update"选项,这让我感到非常惊讶.我在官方文档上什么都没有找到.在一些有相同问题的老帖子上,答案都不是结论性的,而是集中在"on_delete"上.我问的是"ON_UPDATE"选项,仅此而已.那么,该选项在哪里,或者为什么它在Django的ORM中不存在?

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 ?

推荐答案

即使对于

on_delete不会在数据库中创建SQL约束.支持数据库级别的级联选项可能会在以后实现.

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

以下内容具有便利的优势,因为您的应用程序可以对 pre_delete post_delete 信号做出反应,但在性能方面也有一些劣势

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

关于 ON_UPDATE

它没有实现,并且很少使用,因为通常您永远都不会更改模型ID的

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

如果您需要特定于数据库级别的内容或在应用程序级别上进行仿真以更好地控制(例如,覆盖模型保存方法),则仍然可以创建SQL迁移

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

这篇关于为什么要"on_update"?Django Relationships字段中不存在选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆