'SQL Alchemy inspect module giving table names in lower case
Here is how am getting table names for a schema using sql alchemy
from sqlalchemy import inspect
inspector = inspect(engine)
table_list = inspector.get_table_names(schema="PUBLIC")
this gives me table names in lower case but actually tables exist with capital case in database, so i want to get table names with same case in which they exist in database/schema
Note: using snowflake database
Solution 1:[1]
Whether or not SQLAlchemy does "case normalization" is left up to the dialect because normalization can be convenient for some databases and a nuisance for others.
For example, the mssql+pyodbc:// dialect does not force table names to lower case:
with engine.begin() as conn:
conn.exec_driver_sql("DROP TABLE IF EXISTS MY_TABLE")
conn.exec_driver_sql("CREATE TABLE MY_TABLE (id int primary key)")
insp = sa.inspect(engine)
print(insp.get_table_names())
# ['MY_TABLE', 'tbl_main', 'tbl_temp']
You will probably have to ask the developers of snowflake-sqlalchemy about this.
Solution 2:[2]
In snowflake database, I just checked by creating tables with different cases and what i noticed is that if we create table with small/upper case name like (test/TEST) then SqlAlchemy returns name with small case in get_table_name method but if we create table with name having mix cases like (TestAbc) then it returns exact case so i think we can simply convert table names with lower case to upper after getting it from sql alchmey metadata/inspect method
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gord Thompson |
| Solution 2 | Sony Khan |
