Where should I put the code for checking if a table exists in python if I use peewee

I know how to check if a table exists:

create table if not exists TableName (col1 typ1, ..., colN typN)

      

But I don't know where I should put the code, or if there are other ways to check if a table exists.

Please give me an example.

+3


source to share


2 answers


From http://peewee.readthedocs.org/en/latest/peewee/api.html#Model.table_exists you can do for example

if Model.table_exists():
    do stuff

      



or if you do it while creating tables - even easier than http://peewee.readthedocs.org/en/latest/peewee/api.html#Model.create_table

Model.create_table(True)

      

+15


source


When I create an application, I usually put the code to create tables at the entry point of the application. For scripting this if __name__ == '__main__':

, for web applications, some kind of main module is usually used that initializes the wsgi application.



http://charlesleifer.com/blog/structuring-flask-apps-a-how-to-for-those-coming-from-django/

0


source







All Articles