Forums

Sqlalchemy, sqlite3 and Flask works on my desktop but not pythonanywhere

When I run this on my local machine, it works fine. It was running fine on pythonanywhere earlier today. I made some changes that broke it, and I haven't been able to get it to work since, despite deleting all my files and app and starting from scratch.

My first guess was that I accidentally broke my sqlite database, but I've since reverted it to a version that's currently working fine on the local machine.

The error output is:

2015-02-15 01:59:06,395 :sqlalchemy.exc.OperationalError: (OperationalError) no such table: menu_item u'SELECT menu_item.id AS menu_item_id, menu_item.name AS menu_item_name, menu_item.price AS menu_item_price, menu_item.likes AS menu_item_likes, menu_item.description AS menu_item_description, menu_item.menu_category_id AS menu_item_menu_category_id, menu_item.vendor_id AS menu_item_vendor_id \nFROM menu_item ORDER BY menu_item.likes DESC' ()

It seems like it's not reading the database but I don't know why.

from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, \
     render_template, flash, _app_ctx_stack

# configuration
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'

# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from models import Vendor, MenuItem, Review, Base

engine = create_engine('sqlite:///data.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():
    from models import Vendor, MenuItem, Review, Base
    Base.metadata.create_all(bind=engine)

@app.route('/', methods=['GET', 'POST'])
def search_results():
    if request.method == 'POST':
        search = request.form['menuItem']
        entries = db_session.query(MenuItem).filter(MenuItem.name.contains(search)).order_by(MenuItem.likes.desc()).all()
    else:
        entries = db_session.query(MenuItem).order_by(MenuItem.likes.desc()).all()
    return render_template('search_results.html', entries=entries, neighborhood="Crown Heights")


if __name__ == '__main__':
    init_db()
    app.run()

It's not reading the database because you're using a relative path to the database file and that means that it's dependent on the working directory and that can change. Use the full path to your database file. I'm also not sure about the trailing slash in the database filename.

Yeah, I just figured that out. Thanks!

Please help I have the same problem!!

What is the database URI that you're passing in to create_engine?