Skip to content

Commit a333605

Browse files
authored
fix: m2m migrate raises TypeError (#448)
* fix: m2m migrate raises TypeError * tests: check warning message * docs: update changelog
1 parent f0309c9 commit a333605

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

CHANGELOG.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
# ChangeLog
22

3-
## 0.8
3+
## 0.9
4+
### [0.9.0]**(Unreleased)**
45

5-
### [0.8.3]**(Unreleased)**
6+
### Changed
7+
- Drop support for Python3.8. ([#446])
68

79
#### Fixed
10+
- fix: m2m migrate raises TypeError. ([#448])
811
- fix: `aerich init-db` process is suspended. ([#435])
912

13+
[#448]: https://github.com/tortoise/aerich/pull/448
14+
[#446]: https://github.com/tortoise/aerich/pull/446
1015
[#435]: https://github.com/tortoise/aerich/pull/435
1116

17+
## 0.8
18+
1219
### [0.8.2](../../releases/tag/v0.8.2) - 2025-02-28
1320

1421
#### Added

aerich/migrate.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import contextlib
34
import importlib
45
import os
56
from collections.abc import Iterable
@@ -276,14 +277,20 @@ def _handle_m2m_fields(
276277
if attr == "indexed":
277278
# Ignore changing of indexed, as it usually changed by unique
278279
continue
279-
elif attr == "unique":
280-
# TODO:
281-
continue
282280
elif attr == "nullable":
283281
# nullable of m2m relation is constrainted by orm framework, not by db
284282
continue
285-
if change[0][0] == "db_constraint":
286-
continue
283+
elif attr in ("unique", "db_constraint"):
284+
# TODO: handle 'unique'
285+
if upgrade:
286+
click.secho(
287+
f"Aerich does not handle {attr!r} attribution for m2m field. You may need to change the constraints in db manually.",
288+
fg=Color.yellow,
289+
)
290+
continue
291+
with contextlib.suppress(TypeError, KeyError):
292+
if change[0][0] == "db_constraint":
293+
continue
287294
new_value = change[0][1]
288295
if isinstance(new_value, str):
289296
for new_m2m_field in new_m2m_fields:

tests/test_migrate.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ def describe_index(idx: Index) -> Index | dict:
938938
}
939939

940940

941-
def test_migrate(mocker: MockerFixture):
941+
def test_migrate(mocker: MockerFixture, capsys):
942942
"""
943943
models.py diff with old_models.py
944944
- change email pk: id -> email_id
@@ -980,6 +980,7 @@ def test_migrate(mocker: MockerFixture):
980980
Migrate.diff_models(old_models_describe, models_describe)
981981
Migrate.diff_models(models_describe, old_models_describe, False)
982982
Migrate._merge_operators()
983+
warning_msg = "Aerich does not handle 'unique' attribution for m2m field. You may need to change the constraints in db manually."
983984
if isinstance(Migrate.ddl, MysqlDDL):
984985
expected_upgrade_operators = {
985986
"ALTER TABLE `category` MODIFY COLUMN `name` VARCHAR(200)",
@@ -1080,6 +1081,7 @@ def test_migrate(mocker: MockerFixture):
10801081
assert not downgrade_more_than_expected
10811082
downgrade_less_than_expected = expected_downgrade_operators - downgrade_operators
10821083
assert not downgrade_less_than_expected
1084+
assert warning_msg in capsys.readouterr().out
10831085

10841086
elif isinstance(Migrate.ddl, PostgresDDL):
10851087
expected_upgrade_operators = {
@@ -1183,6 +1185,7 @@ def test_migrate(mocker: MockerFixture):
11831185
assert not downgrade_more_than_expected
11841186
downgrade_less_than_expected = expected_downgrade_operators - downgrade_operators
11851187
assert not downgrade_less_than_expected
1188+
assert warning_msg in capsys.readouterr().out
11861189

11871190
elif isinstance(Migrate.ddl, SqliteDDL):
11881191
assert Migrate.upgrade_operators == []

0 commit comments

Comments
 (0)