From 60633ef3dec8421706c610d8238af2cd679fc915 Mon Sep 17 00:00:00 2001 From: Matthew Schinckel Date: Wed, 2 Mar 2016 14:40:46 +1030 Subject: [PATCH] Fixed #26304 -- Ignored unmanaged through model in table introspection. --- AUTHORS | 1 + django/db/backends/base/introspection.py | 5 ++++- tests/introspection/models.py | 9 +++++++++ tests/introspection/tests.py | 6 +++++- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index 81f81e26ec..4a09def5ad 100644 --- a/AUTHORS +++ b/AUTHORS @@ -481,6 +481,7 @@ answer newbie questions, and generally made Django that much better: Matt Deacalion Stevens Matt Dennenbaum Matthew Flanagan + Matthew Schinckel Matthew Somerville Matthew Tretter Matthias Kestenholz diff --git a/django/db/backends/base/introspection.py b/django/db/backends/base/introspection.py index 762cca441f..7f3c7aca59 100644 --- a/django/db/backends/base/introspection.py +++ b/django/db/backends/base/introspection.py @@ -80,7 +80,10 @@ class BaseDatabaseIntrospection(object): if not model._meta.managed: continue tables.add(model._meta.db_table) - tables.update(f.m2m_db_table() for f in model._meta.local_many_to_many) + tables.update( + f.m2m_db_table() for f in model._meta.local_many_to_many + if f.remote_field.through._meta.managed + ) tables = list(tables) if only_existing: existing_tables = self.table_names(include_views=include_views) diff --git a/tests/introspection/models.py b/tests/introspection/models.py index 4d961b20be..f989709e79 100644 --- a/tests/introspection/models.py +++ b/tests/introspection/models.py @@ -45,6 +45,7 @@ class Article(models.Model): body = models.TextField(default='') reporter = models.ForeignKey(Reporter, models.CASCADE) response_to = models.ForeignKey('self', models.SET_NULL, null=True) + unmanaged_reporters = models.ManyToManyField(Reporter, through='ArticleReporter') def __str__(self): return self.headline @@ -54,3 +55,11 @@ class Article(models.Model): index_together = [ ["headline", "pub_date"], ] + + +class ArticleReporter(models.Model): + article = models.ForeignKey(Article, models.CASCADE) + reporter = models.ForeignKey(Reporter, models.CASCADE) + + class Meta: + managed = False diff --git a/tests/introspection/tests.py b/tests/introspection/tests.py index 9ce5a3f0f6..949914ddbf 100644 --- a/tests/introspection/tests.py +++ b/tests/introspection/tests.py @@ -6,7 +6,7 @@ from django.db import connection from django.db.utils import DatabaseError from django.test import TransactionTestCase, mock, skipUnlessDBFeature -from .models import Article, City, Reporter +from .models import Article, ArticleReporter, City, Reporter class IntrospectionTests(TransactionTestCase): @@ -53,6 +53,10 @@ class IntrospectionTests(TransactionTestCase): self.assertNotIn('introspection_article_view', connection.introspection.table_names()) + def test_unmanaged_through_model(self): + tables = connection.introspection.django_table_names() + self.assertNotIn(ArticleReporter._meta.db_table, tables) + def test_installed_models(self): tables = [Article._meta.db_table, Reporter._meta.db_table] models = connection.introspection.installed_models(tables)