1

For some reason flask-sqlalchemy seems to have problems resolving the sqlite tables when running under apache + mod_wsgi.

simplified example:

from models import db, User

app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/zop.sqlite3'
db.init_app(app)

models.py:

from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), unique=True)
    email = db.Column(db.String(120), unique=True)
    ...

Basically my app works fine when I run it via python myapp.py

But when I run this under apache + mod_wsgi, I get the following error when I run something like User.query.filter_by(name=username).first():

OperationalError: (sqlite3.OperationalError) no such table: user [SQL: u'SELECT user.id AS user_id, user.name AS user_name, user.email AS user_email \\nFROM user \\nWHERE user.name = ?\\n LIMIT ? OFFSET ?'] [parameters: ('foo', 1, 0)]

Is there anything I need to change to get it working under apache+mod_wsgi?

shreddd
  • 10,975
  • 9
  • 33
  • 34

1 Answers1

2

OK - figured it out. Leaving the answer here in case someone else gets bitten by this. It turns out that Apache on newer redhat based systems (specifically ones that use systemd) uses a private /tmp.

This is specified in /usr/lib/systemd/system/httpd.service

[Service]
PrivateTmp=true

Because of this, my DB was in /tmp/systemd-private-M4Xj0e/tmp/zop.sqlite3 rather than /tmp/zop.sqlite3. And this DB in the private tmp directory was never properly initialized - running db.create_all() was only creating /tmp/zop.sqlite3 but not /tmp/systemd-private-M4Xj0e/tmp/zop.sqlite3. Apache was throwing errors because it couldn't find the DB.

Using an alternate non-tmp DB location (eg. /var/www/zop.sqlite3) or setting PrivateTmp=false fixes this

More here: http://blog.oddbit.com/2012/11/05/fedora-private-tmp/

shreddd
  • 10,975
  • 9
  • 33
  • 34