4
我似乎無法弄清楚爲什麼我對db.create_all()的調用不起作用。燒瓶SQLAlchemy db.create_all()不創建數據庫
我有一個應用程序包以下初始化:
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
然後,我有應用程序包內models.py:
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包含,除其他事項外,以下內容:
'SQLALCHEMY_DATABASE_URL': 'sqlite:////Users/xxxxx/projects/yyyyy/data-dev.sqlite'
然後,如果我啓動我的交互式shell,你可以看到對象存在適當的ð來電db.create_all()似乎工作,但結果沒有數據庫創建:
$ ./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()
>>>
上爲什麼沒有得到創建該數據庫有什麼想法?
哈,就是這樣。我花了一個小時盯着我的代碼,完全傻眼了,它沒有工作。我不知道所示的SQLAlchemy輸出表示內存數據庫;現在我!謝謝你的第二套眼睛! – David 2014-09-03 15:43:15
如果在正確配置時查看'db.engine',它將顯示您輸入的URI。 – davidism 2014-09-03 15:44:25