Skip to content

Commit 466ddfa

Browse files
authored
fix: model with multi m2m fields generates wrong references name (#1897)
* fix: model with multi m2m fields generates wrong references name * docs: update changelog * Update variable name
1 parent 40b427d commit 466ddfa

File tree

7 files changed

+46
-15
lines changed

7 files changed

+46
-15
lines changed

CHANGELOG.rst

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ Changelog
99
0.24
1010
====
1111

12+
0.24.2 (Unreleased)
13+
------
14+
15+
Fixed
16+
^^^^^
17+
- Fix model with multi m2m fields generates wrong references name (#1897)
18+
1219
0.24.1
1320
------
1421
Added

tests/schema/models_m2m_2.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
This is the testing Models — Multi ManyToMany fields
3+
"""
4+
5+
from __future__ import annotations
6+
7+
from tortoise import Model, fields
8+
9+
10+
class One(Model):
11+
threes: fields.ManyToManyRelation[Three]
12+
13+
14+
class Two(Model):
15+
threes: fields.ManyToManyRelation[Three]
16+
17+
18+
class Three(Model):
19+
ones: fields.ManyToManyRelation[One] = fields.ManyToManyField("models.One")
20+
twos: fields.ManyToManyRelation[Two] = fields.ManyToManyField("models.Two")

tests/schema/test_generate_schema.py

+7
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ async def test_m2m_bad_model_name(self):
217217
):
218218
await self.init_for("tests.schema.models_m2m_1")
219219

220+
async def test_multi_m2m_fields_in_a_model(self):
221+
await self.init_for("tests.schema.models_m2m_2")
222+
sql = self.get_sql("CASCADE")
223+
self.assertNotRegex(sql, r'REFERENCES [`"]three_one[`"]')
224+
self.assertNotRegex(sql, r'REFERENCES [`"]three_two[`"]')
225+
self.assertRegex(sql, r'REFERENCES [`"](one|two|three)[`"]')
226+
220227
async def test_table_and_row_comment_generation(self):
221228
await self.init_for("tests.testmodels")
222229
sql = self.get_sql("comments")

tests/test_order_by.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from tortoise.contrib import test
1010
from tortoise.contrib.test.condition import NotEQ
1111
from tortoise.exceptions import ConfigurationError, FieldError
12-
from tortoise.expressions import Q, Case, When
12+
from tortoise.expressions import Case, Q, When
1313
from tortoise.functions import Count, Sum
1414

1515

tests/test_values.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from tortoise.contrib import test
55
from tortoise.contrib.test.condition import In, NotEQ
66
from tortoise.exceptions import FieldError
7-
from tortoise.expressions import Q, Case, Function, When
7+
from tortoise.expressions import Case, Function, Q, When
88
from tortoise.functions import Length, Trim
99

1010

tortoise/backends/base/schema_generator.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def _get_field_indexes_sqls(
296296
return [val for val in list(dict.fromkeys(indexes)) if val]
297297

298298
def _get_m2m_tables(
299-
self, model: type[Model], table_name: str, safe: bool, models_tables: list[str]
299+
self, model: type[Model], db_table: str, safe: bool, models_tables: list[str]
300300
) -> list[str]:
301301
m2m_tables_for_create = []
302302
for m2m_field in model._meta.m2m_fields:
@@ -308,7 +308,7 @@ def _get_m2m_tables(
308308
backward_fk = self._create_fk_string(
309309
"",
310310
backward_key,
311-
table_name,
311+
db_table,
312312
model._meta.db_pk_column,
313313
field_object.on_delete,
314314
"",
@@ -324,26 +324,23 @@ def _get_m2m_tables(
324324
else:
325325
backward_fk = forward_fk = ""
326326
exists = "IF NOT EXISTS " if safe else ""
327-
table_name = field_object.through
327+
through_table_name = field_object.through
328328
backward_type = self._get_pk_field_sql_type(model._meta.pk)
329329
forward_type = self._get_pk_field_sql_type(field_object.related_model._meta.pk)
330+
comment = ""
331+
if desc := field_object.description:
332+
comment = self._table_comment_generator(table=through_table_name, comment=desc)
330333
m2m_create_string = self.M2M_TABLE_TEMPLATE.format(
331334
exists=exists,
332-
table_name=table_name,
335+
table_name=through_table_name,
333336
backward_fk=backward_fk,
334337
forward_fk=forward_fk,
335338
backward_key=backward_key,
336339
backward_type=backward_type,
337340
forward_key=forward_key,
338341
forward_type=forward_type,
339342
extra=self._table_generate_extra(table=field_object.through),
340-
comment=(
341-
self._table_comment_generator(
342-
table=field_object.through, comment=field_object.description
343-
)
344-
if field_object.description
345-
else ""
346-
),
343+
comment=comment,
347344
)
348345
if not field_object.db_constraint:
349346
m2m_create_string = m2m_create_string.replace(
@@ -355,7 +352,7 @@ def _get_m2m_tables(
355352
m2m_create_string += self._post_table_hook()
356353
if field_object.create_unique_index:
357354
unique_index_create_sql = self._get_unique_index_sql(
358-
exists, table_name, [backward_key, forward_key]
355+
exists, through_table_name, [backward_key, forward_key]
359356
)
360357
if unique_index_create_sql.endswith(";"):
361358
m2m_create_string += "\n" + unique_index_create_sql

tortoise/queryset.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pypika_tortoise.analytics import Count
1010
from pypika_tortoise.functions import Cast
1111
from pypika_tortoise.queries import QueryBuilder
12-
from pypika_tortoise.terms import Case, Field, Star, Term, ValueWrapper, PseudoColumn
12+
from pypika_tortoise.terms import Case, Field, PseudoColumn, Star, Term, ValueWrapper
1313
from typing_extensions import Literal, Protocol
1414

1515
from tortoise.backends.base.client import BaseDBAsyncClient, Capabilities

0 commit comments

Comments
 (0)