From 421e599ad34b93e9537bf4deeca343f5b2e69f06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Sat, 24 Nov 2012 00:44:48 +0200 Subject: [PATCH] [1.5.x] Fixed #19351 -- SQLite bulk_insert of more than 500 single-field objs Backpatch of 0a0a0d66b316598f7c296e8bf75749a14ce3ac49 --- django/db/backends/sqlite3/base.py | 6 +++++- tests/regressiontests/bulk_create/tests.py | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index 7f0c51dd50..6b6c6b67d9 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -120,8 +120,12 @@ class DatabaseOperations(BaseDatabaseOperations): """ SQLite has a compile-time default (SQLITE_LIMIT_VARIABLE_NUMBER) of 999 variables per query. + + If there is just single field to insert, then we can hit another + limit, SQLITE_MAX_COMPOUND_SELECT which defaults to 500. """ - return (999 // len(fields)) if len(fields) > 0 else len(objs) + limit = 999 if len(fields) > 1 else 500 + return (limit // len(fields)) if len(fields) > 0 else len(objs) def date_extract_sql(self, lookup_type, field_name): # sqlite doesn't support extract, so we fake it with the user-defined diff --git a/tests/regressiontests/bulk_create/tests.py b/tests/regressiontests/bulk_create/tests.py index 7cd74d1b47..d4772934a1 100644 --- a/tests/regressiontests/bulk_create/tests.py +++ b/tests/regressiontests/bulk_create/tests.py @@ -102,6 +102,14 @@ class BulkCreateTests(TestCase): 101) self.assertEqual(TwoFields.objects.filter(f2__gte=901).count(), 101) + @skipUnlessDBFeature('has_bulk_insert') + def test_large_single_field_batch(self): + # SQLite had a problem with more than 500 UNIONed selects in single + # query. + Restaurant.objects.bulk_create([ + Restaurant() for i in range(0, 501) + ]) + @skipUnlessDBFeature('has_bulk_insert') def test_large_batch_efficiency(self): with override_settings(DEBUG=True):