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
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ Changelog
0.24
====

0.24.2 (Unreleased)
------

Fixed
^^^^^
- Fix model with multi m2m fields generates wrong references name (#1897)

0.24.1
------
Added
Expand Down
20 changes: 20 additions & 0 deletions tests/schema/models_m2m_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
This is the testing Models — Multi ManyToMany fields
"""

from __future__ import annotations

from tortoise import Model, fields


class One(Model):
threes: fields.ManyToManyRelation[Three]


class Two(Model):
threes: fields.ManyToManyRelation[Three]


class Three(Model):
ones: fields.ManyToManyRelation[One] = fields.ManyToManyField("models.One")
twos: fields.ManyToManyRelation[Two] = fields.ManyToManyField("models.Two")
7 changes: 7 additions & 0 deletions tests/schema/test_generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ async def test_m2m_bad_model_name(self):
):
await self.init_for("tests.schema.models_m2m_1")

async def test_multi_m2m_fields_in_a_model(self):
await self.init_for("tests.schema.models_m2m_2")
sql = self.get_sql("CASCADE")
self.assertNotRegex(sql, r'REFERENCES [`"]three_one[`"]')
self.assertNotRegex(sql, r'REFERENCES [`"]three_two[`"]')
self.assertRegex(sql, r'REFERENCES [`"](one|two|three)[`"]')

async def test_table_and_row_comment_generation(self):
await self.init_for("tests.testmodels")
sql = self.get_sql("comments")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_order_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from tortoise.contrib import test
from tortoise.contrib.test.condition import NotEQ
from tortoise.exceptions import ConfigurationError, FieldError
from tortoise.expressions import Q, Case, When
from tortoise.expressions import Case, Q, When
from tortoise.functions import Count, Sum


Expand Down
2 changes: 1 addition & 1 deletion tests/test_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from tortoise.contrib import test
from tortoise.contrib.test.condition import In, NotEQ
from tortoise.exceptions import FieldError
from tortoise.expressions import Q, Case, Function, When
from tortoise.expressions import Case, Function, Q, When
from tortoise.functions import Length, Trim


Expand Down
21 changes: 9 additions & 12 deletions tortoise/backends/base/schema_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def _get_field_indexes_sqls(
return [val for val in list(dict.fromkeys(indexes)) if val]

def _get_m2m_tables(
self, model: type[Model], table_name: str, safe: bool, models_tables: list[str]
self, model: type[Model], db_table: str, safe: bool, models_tables: list[str]
) -> list[str]:
m2m_tables_for_create = []
for m2m_field in model._meta.m2m_fields:
Expand All @@ -308,7 +308,7 @@ def _get_m2m_tables(
backward_fk = self._create_fk_string(
"",
backward_key,
table_name,
db_table,
model._meta.db_pk_column,
field_object.on_delete,
"",
Expand All @@ -324,26 +324,23 @@ def _get_m2m_tables(
else:
backward_fk = forward_fk = ""
exists = "IF NOT EXISTS " if safe else ""
table_name = field_object.through
through_table_name = field_object.through
backward_type = self._get_pk_field_sql_type(model._meta.pk)
forward_type = self._get_pk_field_sql_type(field_object.related_model._meta.pk)
comment = ""
if desc := field_object.description:
comment = self._table_comment_generator(table=through_table_name, comment=desc)
m2m_create_string = self.M2M_TABLE_TEMPLATE.format(
exists=exists,
table_name=table_name,
table_name=through_table_name,
backward_fk=backward_fk,
forward_fk=forward_fk,
backward_key=backward_key,
backward_type=backward_type,
forward_key=forward_key,
forward_type=forward_type,
extra=self._table_generate_extra(table=field_object.through),
comment=(
self._table_comment_generator(
table=field_object.through, comment=field_object.description
)
if field_object.description
else ""
),
comment=comment,
)
if not field_object.db_constraint:
m2m_create_string = m2m_create_string.replace(
Expand All @@ -355,7 +352,7 @@ def _get_m2m_tables(
m2m_create_string += self._post_table_hook()
if field_object.create_unique_index:
unique_index_create_sql = self._get_unique_index_sql(
exists, table_name, [backward_key, forward_key]
exists, through_table_name, [backward_key, forward_key]
)
if unique_index_create_sql.endswith(";"):
m2m_create_string += "\n" + unique_index_create_sql
Expand Down
2 changes: 1 addition & 1 deletion tortoise/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pypika_tortoise.analytics import Count
from pypika_tortoise.functions import Cast
from pypika_tortoise.queries import QueryBuilder
from pypika_tortoise.terms import Case, Field, Star, Term, ValueWrapper, PseudoColumn
from pypika_tortoise.terms import Case, Field, PseudoColumn, Star, Term, ValueWrapper
from typing_extensions import Literal, Protocol

from tortoise.backends.base.client import BaseDBAsyncClient, Capabilities
Expand Down