在 Flask 中使用 SQLAlchemy 创建数据库
Creating database with SQLAlchemy in Flask
我想知道在 Flask 中创建 SQLAlchemy 数据库需要做什么。根据 documentation,我应该在我的 Flask 应用程序中创建模型,然后转到 Python shell 并使用 db.create_all()。但那是行不通的。
我的 Flask 应用:
import os
import flask
import settings
from flask_sqlalchemy import SQLAlchemy
app = flask.Flask(__name__)
app.config['SESSION_TYPE'] = 'filesystem'
app.secret_key = os.urandom(24)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////database.db'
db = SQLAlchemy(app)
(...)
型号:
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(15), unique = True)
password = db.Column(db.String(15), unique = True)
tasks = db.relationship('Task', backref='author', lazy='dynamic')
def __init__(self, username, password):
self.username = username
self.password = password
class Task(db.Model):
id = db.Column(db.Integer, primary_key = True)
scene = db.Column(db.String(140), nullable = False)
state = db.Column(db.Integer(1), nullable = False)
progress = db.Column(db.Integer(3), nullable = False)
add_date = db.Column(db.DateTime, nullable = False)
start_date = db.Column(db.DateTime, nullable = False)
finish_date = db.Column(db.DateTime, nullable = False)
rendered_scene = db.Column(db.String(140))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
错误代码:
>>> from app import db
>>> db.create_all()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 895, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "C:\Python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 887, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "C:\Python27\lib\site-packages\sqlalchemy\sql\schema.py", line 3420, in create_all
tables=tables)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1727, in_run_visitor
with self._optional_conn_ctx_manager(connection) as conn:
File "C:\Python27\lib\contextlib.py", line 17, in __enter__
return self.gen.next()
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1720, in_optional_conn_ctx_manager
with self.contextual_connect() as conn:
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1910, incontextual_connect
self.pool.connect(),
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 338, in connect
return _ConnectionFairy._checkout(self)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 645, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 440, in checkout
rec = pool._do_get()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 1058, in _do_get
return self._create_connection()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 285, in _create_connection
return _ConnectionRecord(self)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 411, in __init__
self.connection = self.__connect()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 539, in __connect
connection = self.__pool._creator()
File "C:\Python27\lib\site-packages\sqlalchemy\engine\strategies.py", line 96, in connect
connection_invalidated=invalidated
File "C:\Python27\lib\site-packages\sqlalchemy\util\compat.py", line 199, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\strategies.py", line 90, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Python27\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
将没有第一行的模型剪切并粘贴到您的 Flask 应用程序,然后在您的 Flask 应用程序旁边创建一个 python 文件,打开它并写入此文件。
from app import db
db.create_all()
您可以使用 运行 这个文件解决您的问题。
数据库 uri 中的 /
太多了。格式为 dialect+driver://user:pass@host:port/db_name
。对于 SQLite,db_name
是数据库的路径。您已经指定了绝对路径 /database.db
,这意味着您正在尝试在文件系统的根目录中创建数据库。
使用sqlite:///database.db
(相对路径)将在(相对于)当前工作目录中创建数据库。
您可能想要指定一个绝对路径,但要根据项目位置构建它,因为您可以 运行 来自另一个文件夹的应用程序。假设您想在与应用程序设置代码相同的目录中创建数据库,构建一个相对于 __file__
.
的路径
db_path = os.path.join(os.path.dirname(__file__), 'app.db')
db_uri = 'sqlite:///{}'.format(db_path)
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
我想知道在 Flask 中创建 SQLAlchemy 数据库需要做什么。根据 documentation,我应该在我的 Flask 应用程序中创建模型,然后转到 Python shell 并使用 db.create_all()。但那是行不通的。
我的 Flask 应用:
import os
import flask
import settings
from flask_sqlalchemy import SQLAlchemy
app = flask.Flask(__name__)
app.config['SESSION_TYPE'] = 'filesystem'
app.secret_key = os.urandom(24)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////database.db'
db = SQLAlchemy(app)
(...)
型号:
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(15), unique = True)
password = db.Column(db.String(15), unique = True)
tasks = db.relationship('Task', backref='author', lazy='dynamic')
def __init__(self, username, password):
self.username = username
self.password = password
class Task(db.Model):
id = db.Column(db.Integer, primary_key = True)
scene = db.Column(db.String(140), nullable = False)
state = db.Column(db.Integer(1), nullable = False)
progress = db.Column(db.Integer(3), nullable = False)
add_date = db.Column(db.DateTime, nullable = False)
start_date = db.Column(db.DateTime, nullable = False)
finish_date = db.Column(db.DateTime, nullable = False)
rendered_scene = db.Column(db.String(140))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
错误代码:
>>> from app import db
>>> db.create_all()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 895, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "C:\Python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 887, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "C:\Python27\lib\site-packages\sqlalchemy\sql\schema.py", line 3420, in create_all
tables=tables)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1727, in_run_visitor
with self._optional_conn_ctx_manager(connection) as conn:
File "C:\Python27\lib\contextlib.py", line 17, in __enter__
return self.gen.next()
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1720, in_optional_conn_ctx_manager
with self.contextual_connect() as conn:
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1910, incontextual_connect
self.pool.connect(),
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 338, in connect
return _ConnectionFairy._checkout(self)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 645, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 440, in checkout
rec = pool._do_get()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 1058, in _do_get
return self._create_connection()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 285, in _create_connection
return _ConnectionRecord(self)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 411, in __init__
self.connection = self.__connect()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 539, in __connect
connection = self.__pool._creator()
File "C:\Python27\lib\site-packages\sqlalchemy\engine\strategies.py", line 96, in connect
connection_invalidated=invalidated
File "C:\Python27\lib\site-packages\sqlalchemy\util\compat.py", line 199, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\strategies.py", line 90, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Python27\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
将没有第一行的模型剪切并粘贴到您的 Flask 应用程序,然后在您的 Flask 应用程序旁边创建一个 python 文件,打开它并写入此文件。
from app import db
db.create_all()
您可以使用 运行 这个文件解决您的问题。
数据库 uri 中的 /
太多了。格式为 dialect+driver://user:pass@host:port/db_name
。对于 SQLite,db_name
是数据库的路径。您已经指定了绝对路径 /database.db
,这意味着您正在尝试在文件系统的根目录中创建数据库。
使用sqlite:///database.db
(相对路径)将在(相对于)当前工作目录中创建数据库。
您可能想要指定一个绝对路径,但要根据项目位置构建它,因为您可以 运行 来自另一个文件夹的应用程序。假设您想在与应用程序设置代码相同的目录中创建数据库,构建一个相对于 __file__
.
db_path = os.path.join(os.path.dirname(__file__), 'app.db')
db_uri = 'sqlite:///{}'.format(db_path)
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri