Skip to content

Exclude indexs when delete unique constrains #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions sql_server/pyodbc/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
import datetime

from django.db.backends.base.schema import (
BaseDatabaseSchemaEditor, logger, _is_relevant_relation, _related_non_m2m_objects,
BaseDatabaseSchemaEditor,
_is_relevant_relation,
_related_non_m2m_objects,
logger,
)
from django.db.backends.ddl_references import (
Columns, IndexName, Statement as DjStatement, Table,
Columns,
IndexName,
Statement as DjStatement,
Table,
)
from django.db.models import Index
from django.db.models.fields import AutoField, BigAutoField
Expand Down Expand Up @@ -935,7 +941,8 @@ def remove_field(self, model, field):
})
# Drop unique constraints, SQL Server requires explicit deletion
for name, infodict in constraints.items():
if field.column in infodict['columns'] and infodict['unique'] and not infodict['primary_key']:
if (field.column in infodict['columns'] and infodict['unique'] and
not infodict['primary_key'] and not infodict['index']):
self.execute(self.sql_delete_unique % {
"table": self.quote_name(model._meta.db_table),
"name": self.quote_name(name),
Expand Down
22 changes: 22 additions & 0 deletions testapp/migrations/0006_test_remove_onetoone_field_part1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.0.4 on 2020-04-20 14:59

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('testapp', '0005_test_issue45_unique_type_change_part2'),
]

operations = [
migrations.CreateModel(
name='TestRemoveOneToOneFieldModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('a', models.CharField(max_length=50)),
('b', models.OneToOneField(null=True, on_delete=django.db.models.deletion.SET_NULL, to='testapp.TestRemoveOneToOneFieldModel')),
],
),
]
17 changes: 17 additions & 0 deletions testapp/migrations/0007_test_remove_onetoone_field_part2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.0.4 on 2020-04-20 14:59

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('testapp', '0006_test_remove_onetoone_field_part1'),
]

operations = [
migrations.RemoveField(
model_name='testremoveonetoonefieldmodel',
name='b',
),
]
7 changes: 7 additions & 0 deletions testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ class Meta:
a = models.CharField(max_length=51, null=True)
b = models.CharField(max_length=50)
c = models.CharField(max_length=50)


class TestRemoveOneToOneFieldModel(models.Model):
# Fields used for testing removing OneToOne field. Verifies that delete_unique do not try to remove indexes
# thats already is removed.
# b = models.OneToOneField('self', on_delete=models.SET_NULL, null=True)
a = models.CharField(max_length=50)