summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2013-01-28 10:17:56 +0100
committerAymeric Augustin <aymeric.augustin@m4x.org>2013-01-28 10:17:56 +0100
commitf46d7314b50d71a75f51ebae9c5957cb96b11bd9 (patch)
tree92f8cae7c72011609576ddfac414643ccf3c7a8f
parent14d1d504d5c839869a7f612b7d35fa1adf983f5b (diff)
Fixed #19677 -- Introspection of recursive foreign keys under SQLite.
Thanks Simon Charette.
-rw-r--r--django/db/backends/creation.py11
-rw-r--r--django/db/backends/mysql/creation.py2
-rw-r--r--tests/regressiontests/introspection/models.py1
-rw-r--r--tests/regressiontests/introspection/tests.py8
4 files changed, 14 insertions, 8 deletions
diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
index 2baed62855..7304ebb192 100644
--- a/django/db/backends/creation.py
+++ b/django/db/backends/creation.py
@@ -77,7 +77,7 @@ class BaseDatabaseCreation(object):
field_output.append(tablespace_sql)
if f.rel:
ref_output, pending = self.sql_for_inline_foreign_key_references(
- f, known_models, style)
+ model, f, known_models, style)
if pending:
pending_references.setdefault(f.rel.to, []).append(
(model, f))
@@ -116,15 +116,16 @@ class BaseDatabaseCreation(object):
return final_output, pending_references
- def sql_for_inline_foreign_key_references(self, field, known_models, style):
+ def sql_for_inline_foreign_key_references(self, model, field, known_models, style):
"""
Return the SQL snippet defining the foreign key reference for a field.
"""
qn = self.connection.ops.quote_name
- if field.rel.to in known_models:
+ rel_to = field.rel.to
+ if rel_to in known_models or rel_to == model:
output = [style.SQL_KEYWORD('REFERENCES') + ' ' +
- style.SQL_TABLE(qn(field.rel.to._meta.db_table)) + ' (' +
- style.SQL_FIELD(qn(field.rel.to._meta.get_field(
+ style.SQL_TABLE(qn(rel_to._meta.db_table)) + ' (' +
+ style.SQL_FIELD(qn(rel_to._meta.get_field(
field.rel.field_name).column)) + ')' +
self.connection.ops.deferrable_sql()
]
diff --git a/django/db/backends/mysql/creation.py b/django/db/backends/mysql/creation.py
index 53bd57effc..eb31dc66d1 100644
--- a/django/db/backends/mysql/creation.py
+++ b/django/db/backends/mysql/creation.py
@@ -38,7 +38,7 @@ class DatabaseCreation(BaseDatabaseCreation):
suffix.append('COLLATE %s' % self.connection.settings_dict['TEST_COLLATION'])
return ' '.join(suffix)
- def sql_for_inline_foreign_key_references(self, field, known_models, style):
+ def sql_for_inline_foreign_key_references(self, model, field, known_models, style):
"All inline references are pending under MySQL"
return [], True
diff --git a/tests/regressiontests/introspection/models.py b/tests/regressiontests/introspection/models.py
index 4de82e47e7..cfa72c9921 100644
--- a/tests/regressiontests/introspection/models.py
+++ b/tests/regressiontests/introspection/models.py
@@ -23,6 +23,7 @@ class Article(models.Model):
headline = models.CharField(max_length=100)
pub_date = models.DateField()
reporter = models.ForeignKey(Reporter)
+ response_to = models.ForeignKey('self', null=True)
def __str__(self):
return self.headline
diff --git a/tests/regressiontests/introspection/tests.py b/tests/regressiontests/introspection/tests.py
index cd3e1cc8a4..0b4c49077d 100644
--- a/tests/regressiontests/introspection/tests.py
+++ b/tests/regressiontests/introspection/tests.py
@@ -106,13 +106,17 @@ class IntrospectionTests(TestCase):
# should test that the response is correct.
if relations:
# That's {field_index: (field_index_other_table, other_table)}
- self.assertEqual(relations, {3: (0, Reporter._meta.db_table)})
+ self.assertEqual(relations, {3: (0, Reporter._meta.db_table),
+ 4: (0, Article._meta.db_table)})
@skipUnlessDBFeature('can_introspect_foreign_keys')
def test_get_key_columns(self):
cursor = connection.cursor()
key_columns = connection.introspection.get_key_columns(cursor, Article._meta.db_table)
- self.assertEqual(key_columns, [('reporter_id', Reporter._meta.db_table, 'id')])
+ self.assertEqual(
+ set(key_columns),
+ set([('reporter_id', Reporter._meta.db_table, 'id'),
+ ('response_to_id', Article._meta.db_table, 'id')]))
def test_get_primary_key_column(self):
cursor = connection.cursor()