Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #19677 -- Introspection of recursive foreign keys under SQLite.

Thanks Simon Charette.
  • Loading branch information...
commit f46d7314b50d71a75f51ebae9c5957cb96b11bd9 1 parent 14d1d50
@aaugustin aaugustin authored
View
11 django/db/backends/creation.py
@@ -77,7 +77,7 @@ def sql_create_model(self, model, style, known_models=set()):
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 @@ def sql_create_model(self, model, style, known_models=set()):
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()
]
View
2  django/db/backends/mysql/creation.py
@@ -38,7 +38,7 @@ def sql_table_creation_suffix(self):
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
View
1  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
View
8 tests/regressiontests/introspection/tests.py
@@ -106,13 +106,17 @@ def test_get_relations(self):
# 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()
Please sign in to comment.
Something went wrong with that request. Please try again.