烧瓶SQLAlchemy db.create_all()不创建数据库 [英] Flask SQLAlchemy db.create_all() not creating database

查看:149
本文介绍了烧瓶SQLAlchemy db.create_all()不创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚为什么我的db.create_all()调用不起作用。



我有一个包含以下init的应用程序包:

  import Flask 
from config import config $ b $ from flask.ext.sqlalchemy import SQLAlchemy
$ b $创建数据库对象
db = SQLAlchemy()

$这个函数是应用程序工厂
def create_app(environment):
app = Flask(__ name__)
app.config.from_object(config [environment])

bp.init_app(应用程序)

从bp_root导入bp_root
从bp_aws导入bp_aws

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

返回应用程序

然后我在应用程序包中有models.py:

  from datetime import datetime 
from。 import db
$ b 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,电子邮件,用户名):
self.email =电子邮件
self.username =用户名
$ b $ def __repr __(self):
return'< User%r>' %self.username

app.config包含以下内容:

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

/ p>

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

有关为什么数据库没有被创建的想法?

解决方案 c 网址。你可以看到,当你跑这条线时,db没有正确的uri:

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

它显示Flask-SQLAlchemy默认为内存中的sqlite数据库。改变设置,它将工作。


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?

解决方案

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.

这篇关于烧瓶SQLAlchemy db.create_all()不创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆