Opened 10 years ago
Closed 10 years ago
#23713 closed Bug (duplicate)
SQlite3: too many SQL variables, Foreign Keys & Delete
Reported by: | Tanbouz | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.7 |
Severity: | Normal | Keywords: | sqlite cascade delete foreign |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Using:
Python 3.4.2 (default, Oct 23 2014, 12:36:46) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import django >>> django.VERSION (1, 7, 1, 'final', 0) >>> import sqlite3 >>> sqlite3.version '2.6.0' >>> sqlite3.sqlite_version '3.8.7'
Database Structure
# Attempt to delete a row from the Categories table Table-1 Categories Table-2 Groups (FK: Groups -> Categories) Table-3 Items (FK: Items -> Groups) Table-R Features <-> Items (Many to Many) Error happens on a query to delete rows from this table! Table-4 Features
Test code:
from django.db import connection as dbconnection # Pass a model to have its data deleted def clean(db_model): while db_model.objects.count(): row = db_model.objects.all()[:1] # Can't use delete on a slice row = db_model.objects.get(pk = row[0].pk) try: row.delete() except django.db.utils.OperationalError: print(dbconnection.queries)
dbconnection.queries prints several small SELECT queries but the last query is a DELETE query with more than 1000 parameters
{'time': '0.001', 'sql': 'QUERY = \'DELETE FROM "myapp_foreignmodel" WHERE "myapp_foreignmodel"."foreignkey_id" IN (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s.... And so on (1180 total in my case)
Errors:
django.db.utils.OperationalError: too many SQL variables sqlite3.OperationalError: too many SQL variables
The above ticket solves the problem by splitting delete queries into batches. It doesn't seem to include cascaded delete queries aimed at a foreign table.
Change History (2)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
I thought it was included in 1.7, my bad. I did the test again several times using the same data and it worked fine in master. I will figure a workaround for now.
Thanks timgraham
Note that the fix for #16426 isn't included in 1.7. Can you submit a test for Django's test suite that fails on master?