Skip to content

Commit be7e968

Browse files
committed
Applied fix from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff to defer constraint checks when loading fixtures.
- Legacy-Id: 5818
1 parent cbfcd6b commit be7e968

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

django/core/management/commands/loaddata.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ def read(self):
8181
if has_bz2:
8282
compression_types['bz2'] = bz2.BZ2File
8383

84+
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
85+
connection.begin_defer_constraint_checks()
86+
87+
8488
app_module_paths = []
8589
for app in get_apps():
8690
if hasattr(app, '__path__'):
@@ -118,6 +122,9 @@ def read(self):
118122
self.stderr.write(
119123
self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format.\n" %
120124
(fixture_name, format)))
125+
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
126+
connection.end_defer_constraint_checks()
127+
121128
if commit:
122129
transaction.rollback(using=using)
123130
transaction.leave_transaction_management(using=using)
@@ -153,6 +160,9 @@ def read(self):
153160
fixture.close()
154161
self.stderr.write(self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting.\n" %
155162
(fixture_name, humanize(fixture_dir))))
163+
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
164+
connection.end_defer_constraint_checks()
165+
156166
if commit:
157167
transaction.rollback(using=using)
158168
transaction.leave_transaction_management(using=using)
@@ -180,6 +190,10 @@ def read(self):
180190
except Exception:
181191
import traceback
182192
fixture.close()
193+
194+
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
195+
connection.end_defer_constraint_checks()
196+
183197
if commit:
184198
transaction.rollback(using=using)
185199
transaction.leave_transaction_management(using=using)
@@ -199,6 +213,9 @@ def read(self):
199213
self.stderr.write(
200214
self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)\n" %
201215
(fixture_name)))
216+
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
217+
connection.end_defer_constraint_checks()
218+
202219
if commit:
203220
transaction.rollback(using=using)
204221
transaction.leave_transaction_management(using=using)
@@ -219,6 +236,9 @@ def read(self):
219236
for line in sequence_sql:
220237
cursor.execute(line)
221238

239+
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
240+
connection.end_defer_constraint_checks()
241+
222242
if commit:
223243
transaction.commit(using=using)
224244
transaction.leave_transaction_management(using=using)

django/db/backends/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ def _savepoint_commit(self, sid):
6565
return
6666
self.cursor().execute(self.ops.savepoint_commit_sql(sid))
6767

68+
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
69+
def begin_defer_constraint_checks(self):
70+
return None
71+
72+
def end_defer_constraint_checks(self):
73+
return None
74+
6875
def close(self):
6976
if self.connection is not None:
7077
self.connection.close()

django/db/backends/mysql/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,10 @@ def get_server_version(self):
316316
raise Exception('Unable to determine MySQL version from version string %r' % self.connection.get_server_info())
317317
self.server_version = tuple([int(x) for x in m.groups()])
318318
return self.server_version
319+
320+
# from https://code.djangoproject.com/attachment/ticket/3615/defer_constraint_checks.diff
321+
def begin_defer_constraint_checks(self):
322+
self.cursor().execute('SET foreign_key_checks=0')
323+
324+
def end_defer_constraint_checks(self):
325+
self.cursor().execute('SET foreign_key_checks=1')

0 commit comments

Comments
 (0)