Django ORM原始删除查询不删除记录

Yug*_*ari 2 django postgresql django-models django-orm

为了方便起见,我在使用raw_sql查询,以使数据库保持最小,我正在删除多余的记录。通过此查询

#d is from a loop and has values
res=MyModel.objects.raw("DELETE FROM mydb_mymodel WHERE mydb_mymodel.s_type = '%s' and mydb_mymodel.barcode = '%s' and mydb_mymodel.shopcode = '%s' and mydb_mymodel.date = '%s'" ,[d.s_type,d.barcode,d.shopcode,d.date])
Run Code Online (Sandbox Code Playgroud)

它不是删除数据库中的记录,而是

当我这样做res.query并从postgres控制台运行它时,它将起作用!

是的,我可以使用

MyModel.objects.filter(s_type=d.s_type,barcode=d.barcode,
shopcode=d.shopcode,date=d.date).delete()
Run Code Online (Sandbox Code Playgroud)

但是我在raw_sql中缺少什么?

Wil*_*sem 5

一个.raw(..)急于执行,它是,就像大多数Django的ORM查询懒洋洋地执行。因此,它返回一个RawQuerySet带有查询的对象。例如:

>>> User.objects.raw('BLA BLA BLA', [])
<RawQuerySet: BLA BLA BLA>
Run Code Online (Sandbox Code Playgroud)

BLA BLA BLA这样的查询没有任何意义:数据库会在上面出错,但是我们仍然检索到RawQuerySet

您可以通过例如迭代对其进行强制评估,然后得到:

>>> list(User.objects.raw('BLA BLA BLA', []))
Traceback (most recent call last):
  File "/djangotest/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/djangotest/env/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/djangotest/env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/djangotest/env/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/djangotest/env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/djangotest/env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 412, in _query
    rowcount = self._do_query(q)
  File "/djangotest/env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 375, in _do_query
    db.query(q)
  File "/djangotest/env/lib/python3.6/site-packages/MySQLdb/connections.py", line 276, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BLA BLA BLA' at line 1")
Run Code Online (Sandbox Code Playgroud)

因此,对list(..)力进行评估,现在数据库当然会产生错误。但是,即使这是一个有效的DELETE查询,它仍然会引发错误,因为这样的查询不会返回任何记录。

为了进行DELETE调用,Django手册指定您应该使用游标[Django-doc]

from django.db import connection

with connection.cursor() as cursor:
    cursor.execute(
        "DELETE FROM mydb_mymodel WHERE s_type = '%s' AND barcode = '%s' AND shopcode = '%s' AND date = '%s'" ,
        [d.s_type,d.barcode,d.shopcode,d.date]
    )
Run Code Online (Sandbox Code Playgroud)

但我认为将其指定为可能要简单得多:

MyModel.objects.filter(
    s_type=d.s_type,
    barcode=d.barcode,
    shopcode=d.shopcode,
    date=d.date
).delete()
Run Code Online (Sandbox Code Playgroud)

这将构造一个DELETE查询,并正确地序列化参数。一个.delete()查询急切地完成,因此使得讨论上述失误的几率是低了不少:如果ORM正确实现,那么我们就需要有关的忧虑。

  • 是! 你是正确的,查询集是“懒惰的”,你也是正确的关于“报价”的,因为它被格式化为“销售”,在运行时会引发错误。 (2认同)