17

run.py

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

main.py

import database

app = Flask(__name__)

def config():
    app.config.from_object('config.DevConfig')

    # Run SQLAlchemy _that uses app.config_ and add entities if in DEBUG mode
    database.init_db(app)

    import blueprints.auth
    app.register_blueprint(blueprints.auth.auth)

database.py

db = None

def init_db(app):
    global db
    db = SQLAlchemy(app)

    from models import User, Interest, Event

    if app.config['DEBUG']:
        print 'Recreating all db'
        db.create_all() # I DO create everything
        print 'Loading test data'
        ... (here I add some Users and etc. and everything works fine - tests pass)

models.py

from database import db

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

blueprints/auth.py

from models import User

auth = Blueprint('auth', __name__)

@auth.route('/')
def index():
    return str(User.query.get(1).interests)

And so I get

OperationalError: (OperationalError) no such table: user u'SELECT user.id AS user_id, user.username AS user_username, user.email AS user_email, user.passhash AS user_passhash, user.vk_page AS user_vk_page \nFROM user \nWHERE user.id = ?' (1,)

What am I doing wrong?

2
  • 2
    Did the table user actually end up being created in the database? Have you checked with another program? Commented Feb 13, 2014 at 22:33
  • 2
    db.create_all() is only executed in debug mode. Is that intentional?
    – dnll
    Commented Feb 14, 2014 at 13:53

2 Answers 2

24

For anyone trying to use an in memory database:

from sqlalchemy import create_engine
from sqlalchemy.pool import StaticPool

engine = create_engine(
    "sqlite://", 
    connect_args={"check_same_thread": False}, 
    poolclass=StaticPool
)
2
  • 2
    You're the man. This is I think the actual answer for having an in memory DB, instead of just switching to a file.
    – Carnifex
    Commented Apr 30, 2020 at 0:11
  • 3
    Oh my god, you're the guy. poolclass=StaticPool made the difference for me. Thanks. Commented Sep 5, 2021 at 22:52
16

There were few things I had to change to make everything work.

  1. replace DATABASE_URI with SQLALCHEMY_DATABASE_URI parametr in config
  2. replace :memory: sqlite address with /tmp/test.db

Now it works fine.

5
  • 8
    +1000 ... geez, that was frustrating. db.create_all() is $@#&*^#* with in-memory sqlite ... ok ... Commented Sep 20, 2014 at 23:32
  • 15
    Explanation for this behavior: gehrcke.de/2015/05/… Commented May 8, 2015 at 14:03
  • I understand how to complete the first part of the this answer, but I'm not sure what you mean by the second part. I assume the :memory: address needs set equal to SQLALCHEMY_DATABASE_URI, but I am not sure how to do that. What do I need to append to my config.py file?
    – sudosensei
    Commented Jul 17, 2017 at 11:10
  • @sudosensei it just means, don't use in-memory storage :) if you used :memory:before, don't. If you did not, you are good.
    – Ben Usman
    Commented Jul 17, 2017 at 19:02
  • My issue was actually something different altogether. Thank you for the quick reply though!
    – sudosensei
    Commented Jul 17, 2017 at 19:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.