Easily check if table exists with python, sqlalchemy on an sql database
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: City Beneath the Waves Looping
--
Chapters
00:00 Question
00:45 Accepted answer (Score 16)
01:09 Answer 2 (Score 3)
01:26 Thank you
--
Full question
https://stackoverflow.com/questions/6486...
Accepted answer links:
[here]: https://stackoverflow.com/a/64862306/214...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #database #sqlalchemy
#avk47
    --
Music by Eric Matyas
https://www.soundimage.org
Track title: City Beneath the Waves Looping
--
Chapters
00:00 Question
00:45 Accepted answer (Score 16)
01:09 Answer 2 (Score 3)
01:26 Thank you
--
Full question
https://stackoverflow.com/questions/6486...
Accepted answer links:
[here]: https://stackoverflow.com/a/64862306/214...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #database #sqlalchemy
#avk47
ACCEPTED ANSWER
Score 26
With SQLAlchemy 1.4+ you can call has_table by using an inspect object, like so:
import sqlalchemy as sa
# … 
engine = sa.create_engine(connection_uri)
insp = sa.inspect(engine)
print(insp.has_table("team", schema="dbo"))  # True (or False, as the case may be)
For earlier versions of SQLAlchemy, see the other answer here.
ANSWER 2
Score 5
so I made this function based on this idea from the first reposnse:
def table_exists(engine,name):
    ins = inspect(engine)
    ret =ins.dialect.has_table(engine.connect(),name)
    print('Table "{}" exists: {}'.format(name, ret))
    return ret