Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I can't seem to figure out why my call to db.create_all() is not working.

I have an app package with following init:

from flask import Flask
from config import config
from flask.ext.sqlalchemy import SQLAlchemy

# create the database object
db = SQLAlchemy()

# this function is the application factory
def create_app(environment):
    app = Flask(__name__)
    app.config.from_object(config[environment])

    db.init_app(app)

    from bp_root import bp_root
    from bp_aws import bp_aws

    app.register_blueprint(bp_root, url_prefix='/')
    app.register_blueprint(bp_aws, url_prefix='/aws')

    return app

Then I have models.py inside the app package:

from datetime import datetime
from . import db

class MyTestClass(db.Model):
    __tablename__ = 'mytesttable'
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(64), nullable=False, unique=True, index=True)
    username = db.Column(db.String(64), nullable=False, unique=True, index=True)
    is_admin = db.Column(db.Boolean)
    password_hash = db.Column(db.String(128))
    location = db.Column(db.String(64))
    member_since = db.Column(db.DateTime, default=datetime.utcnow)
    bio = db.Column(db.Text())

    def __init__(self, email, username):
        self.email = email
        self.username = username

    def __repr__(self):
        return '<User %r>' % self.username

app.config contains, among other things, the following:

'SQLALCHEMY_DATABASE_URL': 'sqlite:////Users/xxxxx/projects/yyyyy/data-dev.sqlite'

Then if I fire up my interactive shell, you can see objects exist appropriately and call to db.create_all() appears to work, but results in no database creation:

$ ./manage.py shell
>>> from app import db
>>> from app import models
>>> app
<Flask 'app'>
>>> db
<SQLAlchemy engine='sqlite://'>
>>> models
<module 'app.models' from '/Users/xxxxx/projects/yyyyy/app/models.py'>
>>> dir(models)
['MyTestClass', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'datetime', 'db']
>>> db.create_all()
>>> 

Any thoughts on why the database isn't getting created?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

The setting should be SQLALCHEMY_DATABASE_URI, not URL. You can see that the db doesn't have the right uri when you ran this line:

>>> db
<SQLAlchemy engine='sqlite://'>

It shows that Flask-SQLAlchemy defaulted to an in-memory sqlite database. Change the setting and it will work.

share|improve this answer
    
Hah, yep that was it. I spent an hour staring at my code completely dumbfounded that it wasn't working. I wasn't aware that the SQLAlchemy output as shown indicated an in-memory database; now I am! Thanks for the second set of eyes! –  David Sep 3 at 15:43
1  
If you look at db.engine when configured correctly, it will show the uri you entered. –  davidism Sep 3 at 15:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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