Skip to content
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
31 changes: 31 additions & 0 deletions tests/fields/test_m2m.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from tests import testmodels
from tortoise.contrib import test
from tortoise.exceptions import OperationalError
from tortoise.fields import ManyToManyField


class TestManyToManyField(test.TestCase):
Expand Down Expand Up @@ -95,3 +96,33 @@ async def test__add_uninstantiated(self):
OperationalError, r"You should first call .save\(\) on <M2MOne>"
):
await two.one.add(one)

async def test_create_unique_index(self):
message = "Parameter `create_unique_index` is deprecated! Use `unique` instead."
with self.assertWarnsRegex(DeprecationWarning, message):
field = ManyToManyField("models.Foo", create_unique_index=False)
assert field.unique is False
with self.assertWarnsRegex(DeprecationWarning, message):
field = ManyToManyField("models.Foo", create_unique_index=False, unique=True)
assert field.unique is False
with self.assertWarnsRegex(DeprecationWarning, message):
field = ManyToManyField("models.Foo", create_unique_index=True)
assert field.unique is True
with self.assertWarnsRegex(DeprecationWarning, message):
field = ManyToManyField("models.Foo", create_unique_index=True, unique=False)
assert field.unique is True
field = ManyToManyField(
"models.Group",
)
assert field.unique is True
field = ManyToManyField(
"models.Group",
"user_group",
"user_id",
"group_id",
"users",
"CASCADE",
True,
False,
)
assert field.unique is False
40 changes: 20 additions & 20 deletions tests/utils/test_describe_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ def test_describe_model_straight(self):
"python_type": "models.StraightFields",
"generated": False,
"nullable": False,
"unique": False,
"indexed": False,
"unique": True,
"indexed": True,
"default": None,
"description": "M2M to myself",
"docstring": None,
Expand All @@ -350,8 +350,8 @@ def test_describe_model_straight(self):
"python_type": "models.StraightFields",
"generated": False,
"nullable": False,
"unique": False,
"indexed": False,
"unique": True,
"indexed": True,
"default": None,
"description": "M2M to myself",
"docstring": None,
Expand Down Expand Up @@ -558,8 +558,8 @@ def test_describe_model_straight_native(self):
"python_type": StraightFields,
"generated": False,
"nullable": False,
"unique": False,
"indexed": False,
"unique": True,
"indexed": True,
"default": None,
"description": "M2M to myself",
"docstring": None,
Expand All @@ -579,8 +579,8 @@ def test_describe_model_straight_native(self):
"python_type": StraightFields,
"generated": False,
"nullable": False,
"unique": False,
"indexed": False,
"unique": True,
"indexed": True,
"default": None,
"description": "M2M to myself",
"docstring": None,
Expand Down Expand Up @@ -787,8 +787,8 @@ def test_describe_model_source(self):
"python_type": "models.SourceFields",
"generated": False,
"nullable": False,
"unique": False,
"indexed": False,
"unique": True,
"indexed": True,
"default": None,
"description": "M2M to myself",
"docstring": None,
Expand All @@ -808,8 +808,8 @@ def test_describe_model_source(self):
"python_type": "models.SourceFields",
"generated": False,
"nullable": False,
"unique": False,
"indexed": False,
"unique": True,
"indexed": True,
"default": None,
"description": "M2M to myself",
"docstring": None,
Expand Down Expand Up @@ -1016,8 +1016,8 @@ def test_describe_model_source_native(self):
"python_type": SourceFields,
"generated": False,
"nullable": False,
"unique": False,
"indexed": False,
"unique": True,
"indexed": True,
"default": None,
"description": "M2M to myself",
"docstring": None,
Expand All @@ -1037,8 +1037,8 @@ def test_describe_model_source_native(self):
"python_type": SourceFields,
"generated": False,
"nullable": False,
"unique": False,
"indexed": False,
"unique": True,
"indexed": True,
"default": None,
"description": "M2M to myself",
"docstring": None,
Expand Down Expand Up @@ -1117,15 +1117,15 @@ def test_describe_model_uuidpk(self):
"field_type": "ManyToManyFieldInstance",
"forward_key": "uuidm2mrelatedmodel_id",
"generated": False,
"indexed": False,
"indexed": True,
"model_name": "models.UUIDM2MRelatedModel",
"name": "peers",
"nullable": False,
"on_delete": "CASCADE",
"python_type": "models.UUIDM2MRelatedModel",
"related_name": "models",
"through": "uuidm2mrelatedmodel_uuidpkmodel",
"unique": False,
"unique": True,
}
],
},
Expand Down Expand Up @@ -1187,8 +1187,8 @@ def test_describe_model_uuidpk_native(self):
"nullable": False,
"field_type": ManyToManyFieldInstance,
"python_type": UUIDM2MRelatedModel,
"unique": False,
"indexed": False,
"unique": True,
"indexed": True,
"default": None,
"description": None,
"docstring": None,
Expand Down
2 changes: 1 addition & 1 deletion tortoise/backends/base/schema_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def _get_m2m_tables(
"",
) # may have better way
m2m_create_string += self._post_table_hook()
if field_object.create_unique_index:
if field_object.unique:
unique_index_create_sql = self._get_unique_index_sql(
exists, through_table_name, [backward_key, forward_key]
)
Expand Down
20 changes: 13 additions & 7 deletions tortoise/fields/relational.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import warnings
from collections.abc import AsyncGenerator, Generator, Iterator
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -350,12 +351,19 @@ def __init__(
related_name: str = "",
on_delete: OnDelete = CASCADE,
field_type: type[MODEL] = None, # type: ignore
create_unique_index: bool = True,
unique: bool = True,
**kwargs: Any,
) -> None:
# TODO: rename through to through_table
# TODO: add through to use a Model
super().__init__(field_type, **kwargs)
if "create_unique_index" in kwargs:
warnings.warn(
"Parameter `create_unique_index` is deprecated! Use `unique` instead.",
DeprecationWarning,
stacklevel=2,
)
unique = kwargs.pop("create_unique_index")
super().__init__(field_type, unique=unique, **kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to pass unique explicitly here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because ManyToManyFieldInstance inherit from class Field, which receive the unique parameter.

self.validate_model_name(model_name)
self.model_name: str = model_name
self.related_name: str = related_name
Expand All @@ -364,7 +372,6 @@ def __init__(
self.through: str = through # type: ignore
self._generated: bool = False
self.on_delete = on_delete
self.create_unique_index = create_unique_index

def describe(self, serializable: bool) -> dict:
desc = super().describe(serializable)
Expand Down Expand Up @@ -536,7 +543,7 @@ def ManyToManyField(
related_name: str = "",
on_delete: OnDelete = CASCADE,
db_constraint: bool = True,
create_unique_index: bool = True,
unique: bool = True,
**kwargs: Any,
) -> ManyToManyRelation[Any]:
"""
Expand Down Expand Up @@ -582,11 +589,10 @@ def ManyToManyField(
Can only be set is field has a ``default`` set.
``field.NO_ACTION``:
Take no action.
``create_unique_index``:
``unique``:
Controls whether or not a unique index should be created in the database to speed up select queries.
The default is True. If you want to allow repeat records, set this to False.
"""

return ManyToManyFieldInstance( # type: ignore
model_name,
through,
Expand All @@ -595,7 +601,7 @@ def ManyToManyField(
related_name,
on_delete=on_delete,
db_constraint=db_constraint,
create_unique_index=create_unique_index,
unique=unique,
**kwargs,
)

Expand Down