title: Flask + Peewee: where to create tables?
body: <p>Instead of <strong>flask-peewee</strong> I'm using plain <strong>peewee</strong> package.</p>
  
  <p>Here's the way I'm initializing the database:</p>
  
  <pre><code>import os
  
  # just extending the BaseFlask with yaml config reader
  from . extensions.flask_app import Flask
  
  # peewee's wrapper around the database
  from playhouse.flask_utils import FlaskDB
  
  
  db_wrapper = FlaskDB() 
  
  
  # define the application factory
  def create_app(env):
  
      app = Flask(__name__)
  
      # load config depending on the environment
      app.config.from_yaml(os.path.join(app.root_path, 'config.yml'), env)
  
      # init extensions
      db_wrapper.init_app(app)
  
      # ...
  </code></pre>
  
  <p>I know that I should call this to create tables:</p>
  
  <pre><code>from . models import User
  
  db_wrapper.database.connect()
  db_wrapper.database.create_tables([User])
  </code></pre>
  
  <p>But where do I put the <strong>table creation code</strong>, so that the database would be already <strong>initialized</strong>?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Looking at the <a href="http://docs.peewee-orm.com/projects/flask-peewee/en/latest/getting-started.html#adding-users-to-the-site" rel="nofollow noreferrer">docs</a> I found out that I can use <code>User.create_table(fail_silently=True)</code> like that:</p>
  
  <pre><code># in app/__init__.py
  
  # define the application factory
  def create_app(env):
  
      app = Flask(__name__)
  
      # load config depending on the environment
      app.config.from_yaml(os.path.join(app.root_path, 'config.yml'), env)
  
      # init extensions
      db_wrapper.init_app(app)
  
      create_tables();
  
      # rest of the initialization
  
  def create_tables():
  
      from . models import User    
      User.create_table(fail_silently=True)
  </code></pre>
  
  <p>Is it alright to do it here? Or is there a better way/tool for this?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Figured it out. Please, see my answer below.</p>