“Too many SQL variables” error in django with sqlite3

后端 未结 6 1946
臣服心动
臣服心动 2020-12-10 00:23

I\'m getting this error using sqlite3 in django:

Exception Value: too many SQL variables

And I think the answer to it is this, from

6条回答
  •  醉酒成梦
    2020-12-10 01:09

    I had a similar problem, but when deleting just single row that had a large number of related rows in another table. Django was calling a cascading delete causing the problem, but since my code was only calling a single object delete, the solutions described in other answers could not be applied (unless I modified the Django library code, which I didn't want to do).

    My solution was to first delete the related objects via raw sql query, then use the normal Django delete() on the parent object, all wrapped in a transaction.

    Before:

    object.delete() # Django tries to delete a large number of related object and fails
    

    After:

    from django.db import connection
    
    Class MyObject:
        def delete_related(self):
            cur = connection.cursor()
            sql = 'delete from my_table where ...'
            cur.execute(sql, params)
    
    ...
    
    with transaction.atomic():
        object.delete_related() # so that Django won't try (and fail) on its own
        object.delete()
    

提交回复
热议问题