Skip to content

Commit 0d6ae47

Browse files
authored
SNOW-733235: Returning correct exception when table does not exist (#375)
1 parent ab1bd05 commit 0d6ae47

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

DESCRIPTION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Source code is also available at:
1212
- v1.4.7(Unreleased)
1313

1414
- Re-applied the application name of driver connection `SnowflakeConnection` to `SnowflakeSQLAlchemy`.
15+
- `SnowflakeDialect.get_columns` now throws a `NoSuchTableError` exception when the specified table doesn't exist, instead of the more vague `KeyError`.
1516

1617
- v1.4.6(Feb 8, 2023)
1718

src/snowflake/sqlalchemy/snowdialect.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,10 @@ def _get_table_columns(self, connection, table_name, schema=None, **kw):
659659
else False,
660660
}
661661
)
662+
663+
# If we didn't find any columns for the table, the table doesn't exist.
664+
if len(ans) == 0:
665+
raise sa_exc.NoSuchTableError()
662666
return ans
663667

664668
def get_columns(self, connection, table_name, schema=None, **kw):
@@ -673,7 +677,10 @@ def get_columns(self, connection, table_name, schema=None, **kw):
673677
if schema_columns is None:
674678
# Too many results, fall back to only query about single table
675679
return self._get_table_columns(connection, table_name, schema, **kw)
676-
return schema_columns[self.normalize_name(table_name)]
680+
normalized_table_name = self.normalize_name(table_name)
681+
if normalized_table_name not in schema_columns:
682+
raise sa_exc.NoSuchTableError()
683+
return schema_columns[normalized_table_name]
677684

678685
@reflection.cache
679686
def get_table_names(self, connection, schema=None, **kw):

tests/test_core.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
inspect,
3333
text,
3434
)
35-
from sqlalchemy.exc import DBAPIError
35+
from sqlalchemy.exc import DBAPIError, NoSuchTableError
3636
from sqlalchemy.pool import NullPool
3737
from sqlalchemy.sql import and_, not_, or_, select
3838

@@ -406,6 +406,15 @@ def test_insert_tables(engine_testaccount):
406406
users.drop(engine_testaccount)
407407

408408

409+
def test_table_does_not_exist(engine_testaccount):
410+
"""
411+
Tests Correct Exception Thrown When Table Does Not Exist
412+
"""
413+
meta = MetaData()
414+
with pytest.raises(NoSuchTableError):
415+
Table("does_not_exist", meta, autoload=True, autoload_with=engine_testaccount)
416+
417+
409418
@pytest.mark.skip(
410419
"""
411420
Reflection is not implemented yet.

0 commit comments

Comments
 (0)