summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/db/backends/sqlite3/schema.py5
-rw-r--r--docs/releases/4.1.4.txt3
-rw-r--r--tests/schema/tests.py19
3 files changed, 24 insertions, 3 deletions
diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py
index 6c106ae868..88fa466f79 100644
--- a/django/db/backends/sqlite3/schema.py
+++ b/django/db/backends/sqlite3/schema.py
@@ -379,7 +379,10 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
def add_field(self, model, field):
"""Create a field on a model."""
- if (
+ # Special-case implicit M2M tables.
+ if field.many_to_many and field.remote_field.through._meta.auto_created:
+ self.create_model(field.remote_field.through)
+ elif (
# Primary keys and unique fields are not supported in ALTER TABLE
# ADD COLUMN.
field.primary_key
diff --git a/docs/releases/4.1.4.txt b/docs/releases/4.1.4.txt
index da3810875f..21040ebab6 100644
--- a/docs/releases/4.1.4.txt
+++ b/docs/releases/4.1.4.txt
@@ -9,4 +9,5 @@ Django 4.1.4 fixes several bugs in 4.1.3.
Bugfixes
========
-* ...
+* Fixed a regression in Django 4.1 that caused an unnecessary table rebuilt
+ when adding ``ManyToManyField`` on SQLite (:ticket:`34138`).
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 126c03c63c..9c891c015e 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -2112,8 +2112,25 @@ class SchemaTests(TransactionTestCase):
with self.assertRaises(DatabaseError):
self.column_classes(new_field.remote_field.through)
# Add the field
- with connection.schema_editor() as editor:
+ with CaptureQueriesContext(
+ connection
+ ) as ctx, connection.schema_editor() as editor:
editor.add_field(LocalAuthorWithM2M, new_field)
+ # Table is not rebuilt.
+ self.assertEqual(
+ len(
+ [
+ query["sql"]
+ for query in ctx.captured_queries
+ if "CREATE TABLE" in query["sql"]
+ ]
+ ),
+ 1,
+ )
+ self.assertIs(
+ any("DROP TABLE" in query["sql"] for query in ctx.captured_queries),
+ False,
+ )
# Ensure there is now an m2m table there
columns = self.column_classes(new_field.remote_field.through)
self.assertEqual(