5
5
from pathlib import Path
6
6
from typing import TYPE_CHECKING
7
7
8
+ import tortoise
8
9
from tortoise import Tortoise , generate_schema_for_client
9
10
from tortoise .exceptions import OperationalError
10
11
from tortoise .transactions import in_transaction
24
25
)
25
26
26
27
if TYPE_CHECKING :
28
+ from tortoise import Model
29
+ from tortoise .fields .relational import ManyToManyFieldInstance # NOQA:F401
30
+
27
31
from aerich .inspectdb import Inspect
28
32
29
33
@@ -47,7 +51,89 @@ def _init_asyncio_patch():
47
51
set_event_loop_policy (WindowsSelectorEventLoopPolicy ())
48
52
49
53
54
+ def _init_tortoise_0_24_1_patch ():
55
+ # this patch is for "tortoise-orm==0.24.1" to fix:
56
+ # https://github.com/tortoise/tortoise-orm/issues/1893
57
+ if tortoise .__version__ != "0.24.1" :
58
+ return
59
+ from tortoise .backends .base .schema_generator import BaseSchemaGenerator , cast , re
60
+
61
+ def _get_m2m_tables (
62
+ self , model : type [Model ], table_name : str , safe : bool , models_tables : list [str ]
63
+ ) -> list [str ]:
64
+ m2m_tables_for_create = []
65
+ db_table = table_name
66
+ for m2m_field in model ._meta .m2m_fields :
67
+ field_object = cast ("ManyToManyFieldInstance" , model ._meta .fields_map [m2m_field ])
68
+ if field_object ._generated or field_object .through in models_tables :
69
+ continue
70
+ backward_key , forward_key = field_object .backward_key , field_object .forward_key
71
+ if field_object .db_constraint :
72
+ backward_fk = self ._create_fk_string (
73
+ "" ,
74
+ backward_key ,
75
+ db_table ,
76
+ model ._meta .db_pk_column ,
77
+ field_object .on_delete ,
78
+ "" ,
79
+ )
80
+ forward_fk = self ._create_fk_string (
81
+ "" ,
82
+ forward_key ,
83
+ field_object .related_model ._meta .db_table ,
84
+ field_object .related_model ._meta .db_pk_column ,
85
+ field_object .on_delete ,
86
+ "" ,
87
+ )
88
+ else :
89
+ backward_fk = forward_fk = ""
90
+ exists = "IF NOT EXISTS " if safe else ""
91
+ table_name = field_object .through
92
+ backward_type = self ._get_pk_field_sql_type (model ._meta .pk )
93
+ forward_type = self ._get_pk_field_sql_type (field_object .related_model ._meta .pk )
94
+ comment = ""
95
+ if desc := field_object .description :
96
+ comment = self ._table_comment_generator (table = table_name , comment = desc )
97
+ m2m_create_string = self .M2M_TABLE_TEMPLATE .format (
98
+ exists = exists ,
99
+ table_name = table_name ,
100
+ backward_fk = backward_fk ,
101
+ forward_fk = forward_fk ,
102
+ backward_key = backward_key ,
103
+ backward_type = backward_type ,
104
+ forward_key = forward_key ,
105
+ forward_type = forward_type ,
106
+ extra = self ._table_generate_extra (table = field_object .through ),
107
+ comment = comment ,
108
+ )
109
+ if not field_object .db_constraint :
110
+ m2m_create_string = m2m_create_string .replace (
111
+ """,
112
+ ,
113
+ """ ,
114
+ "" ,
115
+ ) # may have better way
116
+ m2m_create_string += self ._post_table_hook ()
117
+ if field_object .create_unique_index :
118
+ unique_index_create_sql = self ._get_unique_index_sql (
119
+ exists , table_name , [backward_key , forward_key ]
120
+ )
121
+ if unique_index_create_sql .endswith (";" ):
122
+ m2m_create_string += "\n " + unique_index_create_sql
123
+ else :
124
+ lines = m2m_create_string .splitlines ()
125
+ lines [- 2 ] += ","
126
+ indent = m .group () if (m := re .match (r"\s+" , lines [- 2 ])) else ""
127
+ lines .insert (- 1 , indent + unique_index_create_sql )
128
+ m2m_create_string = "\n " .join (lines )
129
+ m2m_tables_for_create .append (m2m_create_string )
130
+ return m2m_tables_for_create
131
+
132
+ BaseSchemaGenerator ._get_m2m_tables = _get_m2m_tables
133
+
134
+
50
135
_init_asyncio_patch ()
136
+ _init_tortoise_0_24_1_patch ()
51
137
52
138
53
139
class Command :
0 commit comments