summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2014-05-08 10:33:59 -0700
committerAndrew Godwin <andrew@aeracode.org>2014-05-08 10:34:45 -0700
commitf2bf59a5bc373eeb60136f3b3764e156efbc1ea8 (patch)
tree0a27a7e91b2af5f9091d4b967deabec7e54e59da /tests
parentbc82c0dbac33ced1a4d6953e8626236ae05af956 (diff)
[1.7.x] Fixed #22476: Couldn't alter attributes on M2Ms with through= set
Diffstat (limited to 'tests')
-rw-r--r--tests/schema/models.py16
-rw-r--r--tests/schema/tests.py26
2 files changed, 41 insertions, 1 deletions
diff --git a/tests/schema/models.py b/tests/schema/models.py
index f85a3c6362..f5f59f7416 100644
--- a/tests/schema/models.py
+++ b/tests/schema/models.py
@@ -24,6 +24,22 @@ class AuthorWithM2M(models.Model):
apps = new_apps
+class AuthorWithM2MThrough(models.Model):
+ name = models.CharField(max_length=255)
+ tags = models.ManyToManyField("schema.TagM2MTest", related_name="authors", through="AuthorTag")
+
+ class Meta:
+ apps = new_apps
+
+
+class AuthorTag(models.Model):
+ author = models.ForeignKey("schema.AuthorWithM2MThrough")
+ tag = models.ForeignKey("schema.TagM2MTest")
+
+ class Meta:
+ apps = new_apps
+
+
class Book(models.Model):
author = models.ForeignKey(Author)
title = models.CharField(max_length=100, db_index=True)
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 55b2ee5ce3..371eaadecb 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -9,7 +9,7 @@ from django.db.models.fields.related import ManyToManyField, ForeignKey
from django.db.transaction import atomic
from .models import (Author, AuthorWithM2M, Book, BookWithLongName,
BookWithSlug, BookWithM2M, Tag, TagIndexed, TagM2MTest, TagUniqueRename,
- UniqueTest, Thing, TagThrough, BookWithM2MThrough)
+ UniqueTest, Thing, TagThrough, BookWithM2MThrough, AuthorTag, AuthorWithM2MThrough)
class SchemaTests(TransactionTestCase):
@@ -402,6 +402,30 @@ class SchemaTests(TransactionTestCase):
# Cleanup model states
AuthorWithM2M._meta.local_many_to_many.remove(new_field)
+ def test_m2m_through_alter(self):
+ """
+ Tests altering M2Ms with explicit through models (should no-op)
+ """
+ # Create the tables
+ with connection.schema_editor() as editor:
+ editor.create_model(AuthorTag)
+ editor.create_model(AuthorWithM2MThrough)
+ editor.create_model(TagM2MTest)
+ # Ensure the m2m table is there
+ self.assertEqual(len(self.column_classes(AuthorTag)), 3)
+ # "Alter" the field's blankness. This should not actually do anything.
+ with connection.schema_editor() as editor:
+ old_field = AuthorWithM2MThrough._meta.get_field_by_name("tags")[0]
+ new_field = ManyToManyField("schema.TagM2MTest", related_name="authors", through="AuthorTag")
+ new_field.contribute_to_class(AuthorWithM2MThrough, "tags")
+ editor.alter_field(
+ Author,
+ old_field,
+ new_field,
+ )
+ # Ensure the m2m table is still there
+ self.assertEqual(len(self.column_classes(AuthorTag)), 3)
+
def test_m2m_repoint(self):
"""
Tests repointing M2M fields