Skip to content

Commit d8aeb1e

Browse files
authored
refactor: use 'unique' instead of 'create_unique_index' for m2m field (#1903)
* Use `unique` for m2m field instead of `create_unique_index` * tests: verify warning and exception * refactor: only show warning * Fix test error
1 parent 587d8a0 commit d8aeb1e

File tree

4 files changed

+65
-28
lines changed

4 files changed

+65
-28
lines changed

tests/fields/test_m2m.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from tests import testmodels
22
from tortoise.contrib import test
33
from tortoise.exceptions import OperationalError
4+
from tortoise.fields import ManyToManyField
45

56

67
class TestManyToManyField(test.TestCase):
@@ -95,3 +96,33 @@ async def test__add_uninstantiated(self):
9596
OperationalError, r"You should first call .save\(\) on <M2MOne>"
9697
):
9798
await two.one.add(one)
99+
100+
async def test_create_unique_index(self):
101+
message = "Parameter `create_unique_index` is deprecated! Use `unique` instead."
102+
with self.assertWarnsRegex(DeprecationWarning, message):
103+
field = ManyToManyField("models.Foo", create_unique_index=False)
104+
assert field.unique is False
105+
with self.assertWarnsRegex(DeprecationWarning, message):
106+
field = ManyToManyField("models.Foo", create_unique_index=False, unique=True)
107+
assert field.unique is False
108+
with self.assertWarnsRegex(DeprecationWarning, message):
109+
field = ManyToManyField("models.Foo", create_unique_index=True)
110+
assert field.unique is True
111+
with self.assertWarnsRegex(DeprecationWarning, message):
112+
field = ManyToManyField("models.Foo", create_unique_index=True, unique=False)
113+
assert field.unique is True
114+
field = ManyToManyField(
115+
"models.Group",
116+
)
117+
assert field.unique is True
118+
field = ManyToManyField(
119+
"models.Group",
120+
"user_group",
121+
"user_id",
122+
"group_id",
123+
"users",
124+
"CASCADE",
125+
True,
126+
False,
127+
)
128+
assert field.unique is False

tests/utils/test_describe_model.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ def test_describe_model_straight(self):
329329
"python_type": "models.StraightFields",
330330
"generated": False,
331331
"nullable": False,
332-
"unique": False,
333-
"indexed": False,
332+
"unique": True,
333+
"indexed": True,
334334
"default": None,
335335
"description": "M2M to myself",
336336
"docstring": None,
@@ -350,8 +350,8 @@ def test_describe_model_straight(self):
350350
"python_type": "models.StraightFields",
351351
"generated": False,
352352
"nullable": False,
353-
"unique": False,
354-
"indexed": False,
353+
"unique": True,
354+
"indexed": True,
355355
"default": None,
356356
"description": "M2M to myself",
357357
"docstring": None,
@@ -558,8 +558,8 @@ def test_describe_model_straight_native(self):
558558
"python_type": StraightFields,
559559
"generated": False,
560560
"nullable": False,
561-
"unique": False,
562-
"indexed": False,
561+
"unique": True,
562+
"indexed": True,
563563
"default": None,
564564
"description": "M2M to myself",
565565
"docstring": None,
@@ -579,8 +579,8 @@ def test_describe_model_straight_native(self):
579579
"python_type": StraightFields,
580580
"generated": False,
581581
"nullable": False,
582-
"unique": False,
583-
"indexed": False,
582+
"unique": True,
583+
"indexed": True,
584584
"default": None,
585585
"description": "M2M to myself",
586586
"docstring": None,
@@ -787,8 +787,8 @@ def test_describe_model_source(self):
787787
"python_type": "models.SourceFields",
788788
"generated": False,
789789
"nullable": False,
790-
"unique": False,
791-
"indexed": False,
790+
"unique": True,
791+
"indexed": True,
792792
"default": None,
793793
"description": "M2M to myself",
794794
"docstring": None,
@@ -808,8 +808,8 @@ def test_describe_model_source(self):
808808
"python_type": "models.SourceFields",
809809
"generated": False,
810810
"nullable": False,
811-
"unique": False,
812-
"indexed": False,
811+
"unique": True,
812+
"indexed": True,
813813
"default": None,
814814
"description": "M2M to myself",
815815
"docstring": None,
@@ -1016,8 +1016,8 @@ def test_describe_model_source_native(self):
10161016
"python_type": SourceFields,
10171017
"generated": False,
10181018
"nullable": False,
1019-
"unique": False,
1020-
"indexed": False,
1019+
"unique": True,
1020+
"indexed": True,
10211021
"default": None,
10221022
"description": "M2M to myself",
10231023
"docstring": None,
@@ -1037,8 +1037,8 @@ def test_describe_model_source_native(self):
10371037
"python_type": SourceFields,
10381038
"generated": False,
10391039
"nullable": False,
1040-
"unique": False,
1041-
"indexed": False,
1040+
"unique": True,
1041+
"indexed": True,
10421042
"default": None,
10431043
"description": "M2M to myself",
10441044
"docstring": None,
@@ -1117,15 +1117,15 @@ def test_describe_model_uuidpk(self):
11171117
"field_type": "ManyToManyFieldInstance",
11181118
"forward_key": "uuidm2mrelatedmodel_id",
11191119
"generated": False,
1120-
"indexed": False,
1120+
"indexed": True,
11211121
"model_name": "models.UUIDM2MRelatedModel",
11221122
"name": "peers",
11231123
"nullable": False,
11241124
"on_delete": "CASCADE",
11251125
"python_type": "models.UUIDM2MRelatedModel",
11261126
"related_name": "models",
11271127
"through": "uuidm2mrelatedmodel_uuidpkmodel",
1128-
"unique": False,
1128+
"unique": True,
11291129
}
11301130
],
11311131
},
@@ -1187,8 +1187,8 @@ def test_describe_model_uuidpk_native(self):
11871187
"nullable": False,
11881188
"field_type": ManyToManyFieldInstance,
11891189
"python_type": UUIDM2MRelatedModel,
1190-
"unique": False,
1191-
"indexed": False,
1190+
"unique": True,
1191+
"indexed": True,
11921192
"default": None,
11931193
"description": None,
11941194
"docstring": None,

tortoise/backends/base/schema_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def _get_m2m_tables(
350350
"",
351351
) # may have better way
352352
m2m_create_string += self._post_table_hook()
353-
if field_object.create_unique_index:
353+
if field_object.unique:
354354
unique_index_create_sql = self._get_unique_index_sql(
355355
exists, through_table_name, [backward_key, forward_key]
356356
)

tortoise/fields/relational.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import warnings
34
from collections.abc import AsyncGenerator, Generator, Iterator
45
from typing import (
56
TYPE_CHECKING,
@@ -350,12 +351,19 @@ def __init__(
350351
related_name: str = "",
351352
on_delete: OnDelete = CASCADE,
352353
field_type: type[MODEL] = None, # type: ignore
353-
create_unique_index: bool = True,
354+
unique: bool = True,
354355
**kwargs: Any,
355356
) -> None:
356357
# TODO: rename through to through_table
357358
# TODO: add through to use a Model
358-
super().__init__(field_type, **kwargs)
359+
if "create_unique_index" in kwargs:
360+
warnings.warn(
361+
"Parameter `create_unique_index` is deprecated! Use `unique` instead.",
362+
DeprecationWarning,
363+
stacklevel=2,
364+
)
365+
unique = kwargs.pop("create_unique_index")
366+
super().__init__(field_type, unique=unique, **kwargs)
359367
self.validate_model_name(model_name)
360368
self.model_name: str = model_name
361369
self.related_name: str = related_name
@@ -364,7 +372,6 @@ def __init__(
364372
self.through: str = through # type: ignore
365373
self._generated: bool = False
366374
self.on_delete = on_delete
367-
self.create_unique_index = create_unique_index
368375

369376
def describe(self, serializable: bool) -> dict:
370377
desc = super().describe(serializable)
@@ -536,7 +543,7 @@ def ManyToManyField(
536543
related_name: str = "",
537544
on_delete: OnDelete = CASCADE,
538545
db_constraint: bool = True,
539-
create_unique_index: bool = True,
546+
unique: bool = True,
540547
**kwargs: Any,
541548
) -> ManyToManyRelation[Any]:
542549
"""
@@ -582,11 +589,10 @@ def ManyToManyField(
582589
Can only be set is field has a ``default`` set.
583590
``field.NO_ACTION``:
584591
Take no action.
585-
``create_unique_index``:
592+
``unique``:
586593
Controls whether or not a unique index should be created in the database to speed up select queries.
587594
The default is True. If you want to allow repeat records, set this to False.
588595
"""
589-
590596
return ManyToManyFieldInstance( # type: ignore
591597
model_name,
592598
through,
@@ -595,7 +601,7 @@ def ManyToManyField(
595601
related_name,
596602
on_delete=on_delete,
597603
db_constraint=db_constraint,
598-
create_unique_index=create_unique_index,
604+
unique=unique,
599605
**kwargs,
600606
)
601607

0 commit comments

Comments
 (0)