SQLAlchemy db.create_all() 演示失败

SQLAlchemy db.create_all() demo fails

我正在学习此页面中的教程:

https://pythonhosted.org/Flask-SQLAlchemy/quickstart.html

我相信我是逐行完成的。但是每次尝试 db.create_all()

时都会出错

这是我的代码(为了简单起见,我将实际的 db.create_all() 移动到文件的一部分,尽管我已经按照教程在命令行中显示的方式进行了尝试。

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

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)


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)

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

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

db.create_all()

我在 Stack Overflow 上发现了一个类似的问题 Flask SQLAlchemy db.create_all() not creating database

SQLAlchemy create_all() does not create tables

但那个回应对我没有帮助。 (其实第二个的答案好像有严重bug)

这些都在Python3.4

我得到的错误很长,但这里是:

Traceback (most recent call last):
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\strategies.py", line 90, in connect
    return dialect.connect(*cargs, **cparams)
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\default.py", line 377, in connect
    return self.dbapi.connect(*cargs, **cparams)
sqlite3.OperationalError: unable to open database file

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    import test_create_db.py
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\test_create_db.py", line 25, in <module>
    db.create_all()
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\flask_sqlalchemy\__init__.py", line 895, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\flask_sqlalchemy\__init__.py", line 887, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\sql\schema.py", line 3404, in create_all
    tables=tables)
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\base.py", line 1615, in _run_visitor
    with self._optional_conn_ctx_manager(connection) as conn:
  File "C:\Python34\Lib\contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\base.py", line 1608, in _optional_conn_ctx_manager
    with self.contextual_connect() as conn:
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\base.py", line 1798, in contextual_connect
    self.pool.connect(),
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 338, in connect
    return _ConnectionFairy._checkout(self)
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 644, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 440, in checkout
    rec = pool._do_get()
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 1057, in _do_get
    return self._create_connection()
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 285, in _create_connection
    return _ConnectionRecord(self)
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 411, in __init__
    self.connection = self.__connect()
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\pool.py", line 538, in __connect
    connection = self.__pool._creator()
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\strategies.py", line 96, in connect
    connection_invalidated=invalidated
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\util\compat.py", line 188, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=exc_value)
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\util\compat.py", line 181, in reraise
    raise value.with_traceback(tb)
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\strategies.py", line 90, in connect
    return dialect.connect(*cargs, **cparams)
  File "C:\Programming\LearningFlask\FlaskCookbook\test_create_db\python\lib\site-packages\sqlalchemy\engine\default.py", line 377, in connect
    return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.OperationalError: (OperationalError) unable to open database file None None

您正在使用 Windows,Windows 中没有 /tmp 临时目录。数据库连接失败,因为 SQLite3 没有创建数据库文件的目录。请为您的数据库文件选择一个不同的位置。

在这种情况下,您可以使用 tempfile.gettempdir() 构建路径:

import tempfile
import os.path

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(
    tempfile.gettempdir(), 'test.db')