Skip to content

Commit 76a1317

Browse files
authored
Exclude indexes when deleting unique constraints (#51)
1 parent b5a8b10 commit 76a1317

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

sql_server/pyodbc/schema.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
import datetime
33

44
from django.db.backends.base.schema import (
5-
BaseDatabaseSchemaEditor, logger, _is_relevant_relation, _related_non_m2m_objects,
5+
BaseDatabaseSchemaEditor,
6+
_is_relevant_relation,
7+
_related_non_m2m_objects,
8+
logger,
69
)
710
from django.db.backends.ddl_references import (
8-
Columns, IndexName, Statement as DjStatement, Table,
11+
Columns,
12+
IndexName,
13+
Statement as DjStatement,
14+
Table,
915
)
1016
from django.db.models import Index
1117
from django.db.models.fields import AutoField, BigAutoField
@@ -935,7 +941,8 @@ def remove_field(self, model, field):
935941
})
936942
# Drop unique constraints, SQL Server requires explicit deletion
937943
for name, infodict in constraints.items():
938-
if field.column in infodict['columns'] and infodict['unique'] and not infodict['primary_key']:
944+
if (field.column in infodict['columns'] and infodict['unique'] and
945+
not infodict['primary_key'] and not infodict['index']):
939946
self.execute(self.sql_delete_unique % {
940947
"table": self.quote_name(model._meta.db_table),
941948
"name": self.quote_name(name),
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Generated by Django 3.0.4 on 2020-04-20 14:59
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('testapp', '0005_test_issue45_unique_type_change_part2'),
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='TestRemoveOneToOneFieldModel',
16+
fields=[
17+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('a', models.CharField(max_length=50)),
19+
('b', models.OneToOneField(null=True, on_delete=django.db.models.deletion.SET_NULL, to='testapp.TestRemoveOneToOneFieldModel')),
20+
],
21+
),
22+
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 3.0.4 on 2020-04-20 14:59
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('testapp', '0006_test_remove_onetoone_field_part1'),
10+
]
11+
12+
operations = [
13+
migrations.RemoveField(
14+
model_name='testremoveonetoonefieldmodel',
15+
name='b',
16+
),
17+
]

testapp/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,10 @@ class Meta:
6464
a = models.CharField(max_length=51, null=True)
6565
b = models.CharField(max_length=50)
6666
c = models.CharField(max_length=50)
67+
68+
69+
class TestRemoveOneToOneFieldModel(models.Model):
70+
# Fields used for testing removing OneToOne field. Verifies that delete_unique do not try to remove indexes
71+
# thats already is removed.
72+
# b = models.OneToOneField('self', on_delete=models.SET_NULL, null=True)
73+
a = models.CharField(max_length=50)

0 commit comments

Comments
 (0)