From 12d0a5dad15071952b93916f00d54c3c36a26a66 Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Wed, 11 Dec 2024 21:12:04 +0800 Subject: [PATCH] fix: setting null=false on m2m field causes migration to fail (#385) * fix: setting null=false on m2m field causes migration to fail * Update changelog --- CHANGELOG.md | 1 + aerich/migrate.py | 4 ++-- tests/models.py | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c7f91d..f518021 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### [0.8.1](Unreleased) #### Fixed +- Setting null=false on m2m field causes migration to fail. (#334) - Fix NonExistentKey when running `aerich init` without `[tool]` section in config file. (#284) - Fix configuration file reading error when containing Chinese characters. (#286) - sqlite: failed to create/drop index. (#302) diff --git a/aerich/migrate.py b/aerich/migrate.py index ed1605c..5429ac8 100644 --- a/aerich/migrate.py +++ b/aerich/migrate.py @@ -275,8 +275,8 @@ def diff_models( length = len(old_m2m_fields) field_index = {f["name"]: i for i, f in enumerate(new_m2m_fields)} new_m2m_fields.sort(key=lambda field: field_index.get(field["name"], length)) - for action, _, change in diff(old_m2m_fields, new_m2m_fields): - if change[0][0] == "db_constraint": + for action, option, change in diff(old_m2m_fields, new_m2m_fields): + if (option and option[-1] == "nullable") or change[0][0] == "db_constraint": continue new_value = change[0][1] if isinstance(new_value, str): diff --git a/tests/models.py b/tests/models.py index 0bc4c17..3126fbe 100644 --- a/tests/models.py +++ b/tests/models.py @@ -57,7 +57,9 @@ class Category(Model): class Product(Model): - categories: fields.ManyToManyRelation[Category] = fields.ManyToManyField("models.Category") + categories: fields.ManyToManyRelation[Category] = fields.ManyToManyField( + "models.Category", null=False + ) users: fields.ManyToManyRelation[User] = fields.ManyToManyField( "models.User", related_name="products" )