From 30ec7fe89ac6966c45da11a646553b699cd38624 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sat, 13 Nov 2021 19:22:36 -0500 Subject: [PATCH] Refs #33288 -- Made SQLite introspection raise DatabaseError on nonexistent tables. All the other backends behave this way and we had to make adjustments to our test suite to account for this discrepancy. This also allows SQLite's get_relations() not to raise on a nonexistent table while making sure the InspectDBTestCase.test_introspection_errors test which ensures an error during introspection is surfaced in generated models files still passes. --- django/db/backends/sqlite3/introspection.py | 3 +++ tests/schema/tests.py | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/django/db/backends/sqlite3/introspection.py b/django/db/backends/sqlite3/introspection.py index 87894967fc..bcf11e1751 100644 --- a/django/db/backends/sqlite3/introspection.py +++ b/django/db/backends/sqlite3/introspection.py @@ -3,6 +3,7 @@ from collections import namedtuple import sqlparse +from django.db import DatabaseError from django.db.backends.base.introspection import ( BaseDatabaseIntrospection, FieldInfo as BaseFieldInfo, TableInfo, ) @@ -84,6 +85,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): """ cursor.execute('PRAGMA table_info(%s)' % self.connection.ops.quote_name(table_name)) table_info = cursor.fetchall() + if not table_info: + raise DatabaseError(f'Table {table_name} does not exist (empty pragma).') collations = self._get_column_collations(cursor, table_name) json_columns = set() if self.connection.features.can_introspect_json_field: diff --git a/tests/schema/tests.py b/tests/schema/tests.py index b8ad08239b..253483eb5e 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -119,9 +119,6 @@ class SchemaTests(TransactionTestCase): for name, (type, desc) in columns.items(): if isinstance(type, tuple): columns[name] = (type[0], desc) - # SQLite also doesn't error properly - if not columns: - raise DatabaseError("Table does not exist (empty pragma)") return columns def get_primary_key(self, table):