Skip to content

Commit 97c8992

Browse files
Merge pull request django#16 from akaariai/ticket_18218
Made table_names() output sorted.
2 parents 75743c1 + 527cce8 commit 97c8992

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

django/core/management/commands/inspectdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def handle_inspection(self, options):
4242
yield 'from %s import models' % self.db_module
4343
yield ''
4444
known_models = []
45-
for table_name in connection.introspection.get_table_list(cursor):
45+
for table_name in connection.introspection.table_names(cursor):
4646
yield 'class %s(models.Model):' % table2model(table_name)
4747
known_models.append(table2model(table_name))
4848
try:

django/core/management/sql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def sql_delete(app, style, connection):
6363

6464
# Figure out which tables already exist
6565
if cursor:
66-
table_names = connection.introspection.get_table_list(cursor)
66+
table_names = connection.introspection.table_names(cursor)
6767
else:
6868
table_names = []
6969

django/db/backends/__init__.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -898,10 +898,23 @@ def table_name_converter(self, name):
898898
"""
899899
return name
900900

901-
def table_names(self):
902-
"Returns a list of names of all tables that exist in the database."
903-
cursor = self.connection.cursor()
904-
return self.get_table_list(cursor)
901+
def table_names(self, cursor=None):
902+
"""
903+
Returns a list of names of all tables that exist in the database.
904+
The returned table list is sorted by Python's default sorting. We
905+
do NOT use database's ORDER BY here to avoid subtle differences
906+
in sorting order between databases.
907+
"""
908+
if cursor is None:
909+
cursor = self.connection.cursor()
910+
return sorted(self.get_table_list(cursor))
911+
912+
def get_table_list(self, cursor):
913+
"""
914+
Returns an unsorted list of names of all tables that exist in the
915+
database.
916+
"""
917+
raise NotImplementedError
905918

906919
def django_table_names(self, only_existing=False):
907920
"""

django/db/backends/mysql/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def check_constraints(self, table_names=None):
454454
"""
455455
cursor = self.cursor()
456456
if table_names is None:
457-
table_names = self.introspection.get_table_list(cursor)
457+
table_names = self.introspection.table_names(cursor)
458458
for table_name in table_names:
459459
primary_key_column_name = self.introspection.get_primary_key_column(cursor, table_name)
460460
if not primary_key_column_name:

django/db/backends/sqlite3/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def check_constraints(self, table_names=None):
295295
"""
296296
cursor = self.cursor()
297297
if table_names is None:
298-
table_names = self.introspection.get_table_list(cursor)
298+
table_names = self.introspection.table_names(cursor)
299299
for table_name in table_names:
300300
primary_key_column_name = self.introspection.get_primary_key_column(cursor, table_name)
301301
if not primary_key_column_name:

tests/regressiontests/introspection/tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class IntrospectionTests(TestCase):
4040

4141
def test_table_names(self):
4242
tl = connection.introspection.table_names()
43+
self.assertEqual(tl, sorted(tl))
4344
self.assertTrue(Reporter._meta.db_table in tl,
4445
"'%s' isn't in table_list()." % Reporter._meta.db_table)
4546
self.assertTrue(Article._meta.db_table in tl,

0 commit comments

Comments
 (0)