如何使用 Django 的 ORM 截断表? [英] How to TRUNCATE TABLE using Django's ORM?
本文介绍了如何使用 Django 的 ORM 截断表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要清空数据库表,我使用此 SQL 查询:
To empty a database table, I use this SQL Query:
TRUNCATE TABLE `books`
如何使用 Django 的模型和 ORM 截断表?
How to I truncate a table using Django's models and ORM?
我已经试过了,但它不起作用:
I've tried this, but it doesn't work:
Book.objects.truncate()
推荐答案
最接近 ORM 的是 Book.objects.all().delete()
.
The closest you'll get with the ORM is Book.objects.all().delete()
.
虽然存在差异:截断可能会更快,但 ORM 也会追踪外键引用并删除其他表中的对象.
There are differences though: truncate will likely be faster, but the ORM will also chase down foreign key references and delete objects in other tables.
这篇关于如何使用 Django 的 ORM 截断表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文