Skip to content

Commit 815b19e

Browse files
Merge pull request #8180 from jorisvandenbossche/sql-7815-casesens
SQL: add warning for mismatch in provided and written table name (GH7815)
2 parents fd79860 + 91ce97d commit 815b19e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pandas/io/sql.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,12 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
886886
name, self, frame=frame, index=index, if_exists=if_exists,
887887
index_label=index_label, schema=schema)
888888
table.insert(chunksize)
889+
# check for potentially case sensitivity issues (GH7815)
890+
if name not in self.engine.table_names(schema=schema or self.meta.schema):
891+
warnings.warn("The provided table name '{0}' is not found exactly "
892+
"as such in the database after writing the table, "
893+
"possibly due to case sensitivity issues. Consider "
894+
"using lower case table names.".format(name), UserWarning)
889895

890896
@property
891897
def tables(self):

pandas/io/tests/test_sql.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,19 @@ def test_not_reflect_all_tables(self):
681681
# Verify some things
682682
self.assertEqual(len(w), 0, "Warning triggered for other table")
683683

684+
def test_warning_case_insensitive_table_name(self):
685+
# see GH7815.
686+
# We can't test that this warning is triggered, a the database
687+
# configuration would have to be altered. But here we test that
688+
# the warning is certainly NOT triggered in a normal case.
689+
with warnings.catch_warnings(record=True) as w:
690+
# Cause all warnings to always be triggered.
691+
warnings.simplefilter("always")
692+
# This should not trigger a Warning
693+
self.test_frame1.to_sql('CaseSensitive', self.conn)
694+
# Verify some things
695+
self.assertEqual(len(w), 0, "Warning triggered for writing a table")
696+
684697

685698
class TestSQLLegacyApi(_TestSQLApi):
686699
"""

0 commit comments

Comments
 (0)