Ticket #19676: 0001-Fixed-19676-Avoid-quoting-recursive-ForeignKey-targe.patch

File 0001-Fixed-19676-Avoid-quoting-recursive-ForeignKey-targe.patch, 2.9 KB (added by Simon Charette, 12 years ago)
  • django/core/management/commands/inspectdb.py

    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  
    66
    77from django.core.management.base import NoArgsCommand, CommandError
    88from django.db import connections, DEFAULT_DB_ALIAS
    9 from django.utils import six
     9
    1010
    1111class Command(NoArgsCommand):
    1212    help = "Introspects the database tables in the given database and outputs a Django model module."
    class Command(NoArgsCommand):  
    8686                        extra_params['unique'] = True
    8787
    8888                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])
    9090                    if rel_to in known_models:
    9191                        field_type = 'ForeignKey(%s' % rel_to
    9292                    else:
  • tests/regressiontests/inspectdb/models.py

    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  
    33
    44class People(models.Model):
    55    name = models.CharField(max_length=255)
     6    parent = models.ForeignKey('self')
    67
    78class Message(models.Model):
    89    from_field = models.ForeignKey(People, db_column='from_id')
  • tests/regressiontests/inspectdb/tests.py

    diff --git a/tests/regressiontests/inspectdb/tests.py b/tests/regressiontests/inspectdb/tests.py
    index 028d263..f9cbbce 100644
    a b class InspectDBTestCase(TestCase):  
    3131                     stdout=out)
    3232        output = out.getvalue()
    3333        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)
    3436        self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", output, msg=error_message)
    3537        # As InspectdbPeople model is defined after InspectdbMessage, it should be quoted
    3638        self.assertIn("from_field = models.ForeignKey('InspectdbPeople', db_column='from_id')",
Back to Top