Skip to content

Commit cee3a64

Browse files
FEAT: Support Django 5.1 - Remove index_together usage and update CI/Test Configs (#452)
* Replaced index together with meta.indexes where ever necessary * Updated_changes * debug index_together * modified the code related to the index_together * modifed code --------- Co-authored-by: Gaurav Sharma <sharmag@microsoft.com>
1 parent 9210283 commit cee3a64

File tree

5 files changed

+64
-18
lines changed

5 files changed

+64
-18
lines changed

azure-pipelines.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ jobs:
2424

2525
strategy:
2626
matrix:
27+
Python3.13 - Django 5.1:
28+
python.version: '3.13'
29+
tox.env: 'py313-django51'
30+
Python3.12 - Django 5.1:
31+
python.version: '3.12'
32+
tox.env: 'py312-django51'
33+
Python3.11 - Django 5.1:
34+
python.version: '3.11'
35+
tox.env: 'py311-django51'
36+
Python3.10 - Django 5.1:
37+
python.version: '3.10'
38+
tox.env: 'py310-django51'
39+
2740
Python3.12 - Django 5.0:
2841
python.version: '3.12'
2942
tox.env: 'py312-django50'
@@ -138,6 +151,19 @@ jobs:
138151

139152
strategy:
140153
matrix:
154+
Python3.13 - Django 5.1:
155+
python.version: '3.13'
156+
tox.env: 'py313-django51'
157+
Python3.12 - Django 5.1:
158+
python.version: '3.12'
159+
tox.env: 'py312-django51'
160+
Python3.11 - Django 5.1:
161+
python.version: '3.11'
162+
tox.env: 'py311-django51'
163+
Python3.10 - Django 5.1:
164+
python.version: '3.10'
165+
tox.env: 'py310-django51'
166+
141167
Python3.12 - Django 5.0:
142168
python.version: '3.12'
143169
tox.env: 'py312-django50'

mssql/schema.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,24 @@ def alter_unique_together(self, model, old_unique_together, new_unique_together)
292292
def _model_indexes_sql(self, model):
293293
"""
294294
Return a list of all index SQL statements (field indexes,
295-
index_together, Meta.indexes) for the specified model.
295+
Meta.indexes) for the specified model.
296296
"""
297297
if not model._meta.managed or model._meta.proxy or model._meta.swapped:
298298
return []
299299
output = []
300300
for field in model._meta.local_fields:
301301
output.extend(self._field_indexes_sql(model, field))
302-
303-
for field_names in model._meta.index_together:
304-
fields = [model._meta.get_field(field) for field in field_names]
305-
output.append(self._create_index_sql(model, fields, suffix="_idx"))
302+
# meta.index_together is removed in Django 5.1, so add a version check to handle compatibility
303+
if django_version < (5, 1):
304+
# Iterate over each set of field names defined in index_together
305+
for field_names in model._meta.index_together:
306+
# Get the actual field objects for each field name
307+
fields = [model._meta.get_field(field) for field in field_names]
308+
# Generate the SQL statement to create the index for these fields
309+
sql=self._create_index_sql(model, fields, suffix="_idx")
310+
# If SQL was generated (not None), add it to the output list
311+
if sql:
312+
output.append(sql)
306313

307314
if django_version >= (4, 0):
308315
for field_names in model._meta.unique_together:
@@ -803,10 +810,15 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
803810
if old_field.db_index and new_field.db_index:
804811
index_columns.append([old_field])
805812
else:
806-
for fields in model._meta.index_together:
807-
columns = [model._meta.get_field(field) for field in fields]
808-
if old_field.column in [c.column for c in columns]:
809-
index_columns.append(columns)
813+
# Handle index_together for only django version < 5.1
814+
if django_version < (5, 1):
815+
# Get the field objects for each field name in the index_together.
816+
for fields in model._meta.index_together:
817+
# If the old field's column is among the columns for this index,
818+
# add this set of columns to index_columns for later index recreation.
819+
columns = [model._meta.get_field(field) for field in fields]
820+
if old_field.column in [c.column for c in columns]:
821+
index_columns.append(columns)
810822
if index_columns:
811823
for columns in index_columns:
812824
create_index_sql_statement = self._create_index_sql(model, columns)
@@ -935,10 +947,15 @@ def _delete_indexes(self, model, old_field, new_field):
935947
index_columns.append([old_field.column])
936948
elif old_field.null != new_field.null:
937949
index_columns.append([old_field.column])
938-
for fields in model._meta.index_together:
939-
columns = [model._meta.get_field(field).column for field in fields]
940-
if old_field.column in columns:
941-
index_columns.append(columns)
950+
# Handle index_together for only django version < 5.1
951+
if django_version < (5, 1):
952+
# Iterate over each set of field names defined in index_together
953+
for fields in model._meta.index_together:
954+
# Get the actual column names for each field in the set
955+
columns = [model._meta.get_field(field).column for field in fields]
956+
# If the old field's column is among these columns, add to index_columns for later index deletion
957+
if old_field.column in columns:
958+
index_columns.append(columns)
942959

943960
for index in model._meta.indexes:
944961
columns = [model._meta.get_field(field).column for field in index.fields]

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'Framework :: Django :: 4.1',
2222
'Framework :: Django :: 4.2',
2323
'Framework :: Django :: 5.0',
24+
'Framework :: Django :: 5.1',
2425
]
2526

2627
this_directory = path.abspath(path.dirname(__file__))
@@ -42,7 +43,7 @@
4243
license='BSD',
4344
packages=find_packages(),
4445
install_requires=[
45-
'django>=3.2,<5.1',
46+
'django>=3.2,<5.2',
4647
'pyodbc>=3.0',
4748
'pytz',
4849
],

testapp/tests/test_indexes.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ def test_correct_indexes_exist(self):
116116
expected_index_causes = []
117117
if field.db_index:
118118
expected_index_causes.append('db_index=True')
119-
for field_names in model_cls._meta.index_together:
120-
if field.name in field_names:
121-
expected_index_causes.append(f'index_together[{field_names}]')
119+
if VERSION < (5, 1):
120+
for field_names in model_cls._meta.index_together:
121+
if field.name in field_names:
122+
expected_index_causes.append(f'index_together[{field_names}]')
122123
if field._unique and field.null:
123124
# This is implemented using a (filtered) unique index (not a constraint) to get ANSI NULL behaviour
124125
expected_index_causes.append('unique=True & null=True')
@@ -363,4 +364,4 @@ def test_drop_foreignkey(self):
363364
)
364365
# Test alter fk with fk_on_delete_keep_index in db_comment
365366
# Index should be preserved in this case
366-
self.assertIsNotNone(car_index)
367+
self.assertIsNotNone(car_index)

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ deps =
2323
django41: django>=4.1a1,<4.2
2424
django42: django>=4.2,<4.3
2525
django50: django>=5.0,<5.1
26+
django51: django>=5.1,<5.2

0 commit comments

Comments
 (0)