Skip to content

Commit 4ae1e24

Browse files
authored
Adds unit test for issues #110 and #90 (#115)
* Added test to test changing from non-nullable to nullable with unique index * Fixed failing unit test on Django 3.1 and lower due to bad import
1 parent c578600 commit 4ae1e24

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Generated by Django 3.2.12 on 2022-03-14 18:36
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', '0017_binarydata_testcheckconstraintwithunicode_and_more'),
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Question',
16+
fields=[
17+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('question_text', models.CharField(max_length=200)),
19+
('pub_date', models.DateTimeField(verbose_name='date published')),
20+
],
21+
),
22+
migrations.CreateModel(
23+
name='Choice',
24+
fields=[
25+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
26+
('choice_text', models.CharField(max_length=200)),
27+
('votes', models.IntegerField(default=0)),
28+
('question', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='testapp.question')),
29+
],
30+
options={
31+
'unique_together': {('question', 'choice_text')},
32+
},
33+
),
34+
]

testapp/models.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,23 @@ class Meta:
180180
name='name_does_not_starts_with_\u00f7',
181181
)
182182
]
183+
184+
185+
class Question(models.Model):
186+
question_text = models.CharField(max_length=200)
187+
pub_date = models.DateTimeField('date published')
188+
189+
def __str__(self):
190+
return self.question_text
191+
192+
def was_published_recently(self):
193+
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
194+
195+
196+
class Choice(models.Model):
197+
question = models.ForeignKey(Question, on_delete=models.CASCADE, null=True)
198+
choice_text = models.CharField(max_length=200)
199+
votes = models.IntegerField(default=0)
200+
201+
class Meta:
202+
unique_together = (('question', 'choice_text'))

testapp/tests/test_indexes.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
import logging
22

33
import django.db
4+
from django import VERSION
45
from django.apps import apps
6+
from django.db import models
57
from django.db.models import UniqueConstraint
8+
from django.db.utils import DEFAULT_DB_ALIAS, ConnectionHandler, ProgrammingError
69
from django.test import TestCase
710

811
from ..models import (
9-
TestIndexesRetainedRenamed
12+
TestIndexesRetainedRenamed,
13+
Choice,
14+
Question,
1015
)
1116

17+
connections = ConnectionHandler()
18+
19+
if (VERSION >= (3, 2)):
20+
from django.utils.connection import ConnectionProxy
21+
connection = ConnectionProxy(connections, DEFAULT_DB_ALIAS)
22+
else:
23+
from django.db import DefaultConnectionProxy
24+
connection = DefaultConnectionProxy()
1225

1326
logger = logging.getLogger('mssql.tests')
1427

@@ -139,3 +152,33 @@ def test_correct_indexes_exist(self):
139152
'\n'.join(str(i) for i in col_indexes),
140153
)
141154
logger.debug(' Found %s index(es) as expected', len(col_indexes))
155+
156+
157+
class TestIndexesBeingDropped(TestCase):
158+
159+
def test_unique_index_dropped(self):
160+
"""
161+
Issues https://github.com/microsoft/mssql-django/issues/110
162+
and https://github.com/microsoft/mssql-django/issues/90
163+
Unique indexes not being dropped when changing non-nullable
164+
foreign key with unique_together to nullable causing
165+
dependent on column error
166+
"""
167+
old_field = Choice._meta.get_field('question')
168+
new_field = models.ForeignKey(
169+
Question, null=False, on_delete=models.deletion.CASCADE
170+
)
171+
new_field.set_attributes_from_name("question")
172+
with connection.schema_editor() as editor:
173+
editor.alter_field(Choice, old_field, new_field, strict=True)
174+
175+
old_field = new_field
176+
new_field = models.ForeignKey(
177+
Question, null=True, on_delete=models.deletion.CASCADE
178+
)
179+
new_field.set_attributes_from_name("question")
180+
try:
181+
with connection.schema_editor() as editor:
182+
editor.alter_field(Choice, old_field, new_field, strict=True)
183+
except ProgrammingError:
184+
self.fail("Unique indexes not being dropped")

0 commit comments

Comments
 (0)