title: Google App Engine - Flask - SQLAlchemy - Inconsistent Database
body: <p>On my local server the application database runs fine and always brings up the same results but when I deploy my Flask application to the server and I add an item the results vary when I retrieve the data. You can view this problem on <a href="http://rest-menuapp.appspot.com/JSON" rel="nofollow noreferrer">http://rest-menuapp.appspot.com/JSON</a>. If you refresh the page you will see that sometimes the item is not there and sometimes it is there.</p>
  
  <p>Here is my app.yaml file:</p>
  
  <pre><code>runtime: python
  env: flex
  entrypoint: gunicorn -b :$PORT item_catalog:app
  threadsafe: false
  runtime_config:
    python_version: 2
  
  handlers:
  - url: /static
    static_dir: static
  
  - url: /static/photos
    static_dir: static/photos
  
  - url: /.*
    script: item_catalog.app
  </code></pre>
  
  <p>Here is my database</p>
  
  <pre><code>from sqlalchemy import create_engine
  from sqlalchemy.orm import scoped_session, sessionmaker
  from sqlalchemy.ext.declarative import declarative_base
  
  
  engine = create_engine('sqlite:///catalog.db', convert_unicode=True)
  db_session = scoped_session(sessionmaker(autocommit=False,
                                           autoflush=False,
                                           bind=engine))
  
  Base = declarative_base()
  Base.query = db_session.query_property()
  
  def init_db():
      # import all modules here that might define models so that
      # they will be registered properly on the metadata.  Otherwise
      # you will have to import them first before calling init_db()
      import models
      Base.metadata.create_all(bind=engine)
  </code></pre>
  
  <p>This is the start of my routing python file</p>
  
  <pre><code>from flask import Flask, render_template, request, redirect, jsonify,\
       url_for, session, g, send_from_directory
  from sqlalchemy import create_engine, asc
  from sqlalchemy.orm import sessionmaker
  from datetime import datetime
  from functools import wraps
  from werkzeug.utils import secure_filename
  from PIL import Image
  import os
  
  
  
  from apiclient import discovery
  import httplib2
  from oauth2client import client, crypt
  
  from database import init_db, db_session
  from models import Item, Category, User, Photo
  
  UPLOAD_FOLDER = 'static/photos'
  ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
  init_db()
  
  
  app = Flask(__name__)
  </code></pre>