Skip to content

Commit 53dee7b

Browse files
committed
Disable all constraints when dropping table
1 parent b377347 commit 53dee7b

File tree

2 files changed

+4
-25
lines changed

2 files changed

+4
-25
lines changed

sql_server/pyodbc/base.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -480,18 +480,11 @@ def check_constraints(self, table_names=None):
480480
table_names)
481481

482482
def disable_constraint_checking(self):
483-
# Azure SQL Database doesn't support sp_msforeachtable
484-
# cursor.execute('EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT ALL"')
485-
if not self.needs_rollback:
486-
self._execute_foreach('ALTER TABLE %s NOCHECK CONSTRAINT ALL')
487-
return not self.needs_rollback
483+
self.cursor().execute('EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT ALL"')
484+
return True
488485

489486
def enable_constraint_checking(self):
490-
# Azure SQL Database doesn't support sp_msforeachtable
491-
# cursor.execute('EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL"')
492-
if not self.needs_rollback:
493-
self.check_constraints()
494-
487+
self.cursor().execute('EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL"')
495488

496489
class CursorWrapper(object):
497490
"""

sql_server/pyodbc/schema.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -330,16 +330,14 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
330330
for constraint_name in constraint_names:
331331
self.execute(self._delete_constraint_sql(self.sql_delete_check, model, constraint_name))
332332
# Have they renamed the column?
333-
old_create_indexes_sql = []
334333
if old_field.column != new_field.column:
335334
# remove old indices
336-
old_create_indexes_sql = self._model_indexes_sql(model)
337335
self._delete_indexes(model, old_field, new_field)
338336

339337
self.execute(self._rename_field_sql(model._meta.db_table, old_field, new_field, new_type))
340338
# Rename all references to the renamed column.
341339
for sql in self.deferred_sql:
342-
if isinstance(sql, Statement):
340+
if isinstance(sql, DjStatement):
343341
sql.rename_column_references(model._meta.db_table, old_field.column, new_field.column)
344342

345343
# Next, start accumulating actions to do
@@ -553,13 +551,6 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
553551
}
554552
self.execute(sql, params)
555553

556-
for o in old_create_indexes_sql:
557-
o = str(o)
558-
if old_field.column not in o:
559-
continue
560-
o = o.replace('[%s]' % old_field.column, '[%s]' % new_field.column)
561-
self.execute(o)
562-
563554
# Reset connection if required
564555
if self.connection.features.connection_persists_old_columns:
565556
self.connection.close()
@@ -587,11 +578,6 @@ def _delete_unique_constraints(self, model, old_field, new_field, strict=False):
587578
unique_columns = []
588579
if old_field.unique and new_field.unique:
589580
unique_columns.append([old_field.column])
590-
else:
591-
for fields in model._meta.unique_together:
592-
columns = [model._meta.get_field(field).column for field in fields]
593-
if old_field.column in columns:
594-
unique_columns.append(columns)
595581
if unique_columns:
596582
for columns in unique_columns:
597583
constraint_names = self._constraint_names(model, columns, unique=True)

0 commit comments

Comments
 (0)