Created
August 16, 2010 15:18
-
-
Save rctay/527113 to your computer and use it in GitHub Desktop.
[django] check if db table exists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Came up with this for satchmo's downloadable product migration, 0001_split. | |
""" | |
def db_table_exists(table, cursor=None): | |
try: | |
if not cursor: | |
from django.db import connection | |
cursor = connection.cursor() | |
if not cursor: | |
raise Exception | |
table_names = connection.introspection.get_table_list(cursor) | |
except: | |
raise Exception("unable to determine if the table '%s' exists" % table) | |
else: | |
return table in table_names |
It helps thank you!!!!
Use of connection
has been deprecated. This appears to be the most up to date version of this check:
from django.db import connections
def table_exists(table_name: str, connection_name: str) -> bool:
return table_name in connections[connection_name].introspection.table_names()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's helpful~ thanks.