diff options
| author | Claude Paroz <claude@2xlibre.net> | 2012-04-27 06:56:31 +0000 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2012-04-27 06:56:31 +0000 |
| commit | ddc5d59c6a547f76797d99510df8c3cec61e5f89 (patch) | |
| tree | d59bf49435553f9d421fb09439fecf5d1a15f6b4 | |
| parent | c34d069a75dc3486b984928745b06b8d39767475 (diff) | |
Fixed #15076 -- Quoted ForeignKey target class names in inspectdb when class is defined below.
Thanks saschwarz for the report, jeff@deserettechnology.com for the initial patch and Ramiro Morales for the review.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17942 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/core/management/commands/inspectdb.py | 9 | ||||
| -rw-r--r-- | tests/regressiontests/inspectdb/tests.py | 3 |
2 files changed, 10 insertions, 2 deletions
diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py index 8da0d7e84f..af2da45154 100644 --- a/django/core/management/commands/inspectdb.py +++ b/django/core/management/commands/inspectdb.py @@ -41,8 +41,10 @@ class Command(NoArgsCommand): yield '' yield 'from %s import models' % self.db_module yield '' + known_models = [] for table_name in connection.introspection.get_table_list(cursor): yield 'class %s(models.Model):' % table2model(table_name) + known_models.append(table2model(table_name)) try: relations = connection.introspection.get_relations(cursor, table_name) except NotImplementedError: @@ -83,7 +85,12 @@ class Command(NoArgsCommand): if i in relations: rel_to = relations[i][1] == table_name and "'self'" or table2model(relations[i][1]) - field_type = 'ForeignKey(%s' % rel_to + + if rel_to in known_models: + field_type = 'ForeignKey(%s' % rel_to + else: + field_type = "ForeignKey('%s'" % rel_to + if att_name.endswith('_id'): att_name = att_name[:-3] else: diff --git a/tests/regressiontests/inspectdb/tests.py b/tests/regressiontests/inspectdb/tests.py index 944eb64568..6896bf9126 100644 --- a/tests/regressiontests/inspectdb/tests.py +++ b/tests/regressiontests/inspectdb/tests.py @@ -12,7 +12,8 @@ class InspectDBTestCase(TestCase): call_command('inspectdb', stdout=out) error_message = "inspectdb generated an attribute name which is a python keyword" self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", out.getvalue(), msg=error_message) - self.assertIn("from_field = models.ForeignKey(InspectdbPeople)", out.getvalue()) + # As InspectdbPeople model is defined after InspectdbMessage, it should be quoted + self.assertIn("from_field = models.ForeignKey('InspectdbPeople')", out.getvalue()) self.assertIn("people_pk = models.ForeignKey(InspectdbPeople, primary_key=True)", out.getvalue()) self.assertIn("people_unique = models.ForeignKey(InspectdbPeople, unique=True)", |
