From d9a3a83953fa8d3c0b3f5bb7f5a0515a9077e0c3 Mon Sep 17 00:00:00 2001
From: Simon Charette <charette.s@gmail.com>
Date: Sun, 27 Jan 2013 23:38:39 -0500
Subject: [PATCH] Fixed #19676 -- Avoid quoting recursive ForeignKey target
twice in inspectdb.
The regression was introduced by ddc5d59c6a547f76797d99510df8c3cec61e5f89.
---
django/core/management/commands/inspectdb.py | 4 ++--
tests/regressiontests/inspectdb/models.py | 1 +
tests/regressiontests/inspectdb/tests.py | 2 ++
3 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py
index 936d5e8..f6804bd 100644
a
|
b
|
from optparse import make_option
|
6 | 6 | |
7 | 7 | from django.core.management.base import NoArgsCommand, CommandError |
8 | 8 | from django.db import connections, DEFAULT_DB_ALIAS |
9 | | from django.utils import six |
| 9 | |
10 | 10 | |
11 | 11 | class Command(NoArgsCommand): |
12 | 12 | help = "Introspects the database tables in the given database and outputs a Django model module." |
… |
… |
class Command(NoArgsCommand):
|
86 | 86 | extra_params['unique'] = True |
87 | 87 | |
88 | 88 | if is_relation: |
89 | | rel_to = relations[i][1] == table_name and "'self'" or table2model(relations[i][1]) |
| 89 | rel_to = relations[i][1] == table_name and "self" or table2model(relations[i][1]) |
90 | 90 | if rel_to in known_models: |
91 | 91 | field_type = 'ForeignKey(%s' % rel_to |
92 | 92 | else: |
diff --git a/tests/regressiontests/inspectdb/models.py b/tests/regressiontests/inspectdb/models.py
index 4a66214..a8adf86 100644
a
|
b
|
from django.db import models
|
3 | 3 | |
4 | 4 | class People(models.Model): |
5 | 5 | name = models.CharField(max_length=255) |
| 6 | parent = models.ForeignKey('self') |
6 | 7 | |
7 | 8 | class Message(models.Model): |
8 | 9 | from_field = models.ForeignKey(People, db_column='from_id') |
diff --git a/tests/regressiontests/inspectdb/tests.py b/tests/regressiontests/inspectdb/tests.py
index 028d263..f9cbbce 100644
a
|
b
|
class InspectDBTestCase(TestCase):
|
31 | 31 | stdout=out) |
32 | 32 | output = out.getvalue() |
33 | 33 | error_message = "inspectdb generated an attribute name which is a python keyword" |
| 34 | # Recursive foreign keys should be set to 'self' |
| 35 | self.assertIn("parent = models.ForeignKey('self')", output) |
34 | 36 | self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", output, msg=error_message) |
35 | 37 | # As InspectdbPeople model is defined after InspectdbMessage, it should be quoted |
36 | 38 | self.assertIn("from_field = models.ForeignKey('InspectdbPeople', db_column='from_id')", |