[英]Flask-SQLAlchemy's create_all does not create tables
I'm working on a flask application where I'm trying to isolate my unit tests. 我正在尝试在烧瓶应用程序中尝试隔离单元测试。 I'm using flask-sqlalchemy, and I'm trying to use the
create_all
and drop_all
methods to clean my database after running a test. 我正在使用flask-sqlalchemy,并且试图在运行测试后使用
create_all
和drop_all
方法来清理数据库。
However, it appears my create_all
and drop_all
methods do not actually create/drop the tables as the documentation states. 但是,看来我的
create_all
和drop_all
方法实际上并未按照文档说明创建/删除表。 I have my models imported in the application before calling create_all
, like most other answers say. 像大多数其他答案一样,我在调用
create_all
之前将模型导入了应用程序。
This is the error I'm getting with the code below: 这是我在下面的代码中遇到的错误:
psycopg2.ProgrammingError: relation "tasks" does not exist
Here's my relevant code 这是我的相关代码
/app.py
import os
import configparser
from flask import Flask
from src.router import router
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
if not os.path.exists(os.path.join(app.root_path, 'config.ini')):
raise Exception(f'config.ini not found in the {app.root_path}')
config = configparser.ConfigParser()
config.read('config.ini')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = config[os.environ['APP_ENV']]['DATABASE_URI']
app.register_blueprint(router)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
if __name__ == "__main__":
app.run()
/tests/test_router.py
from unittest import TestCase
from flask import Flask
from app import app, db
from src.models import Task
class TestRouter(TestCase):
def setUp(self):
db.create_all()
def tearDown(self):
db.drop_all()
def test_adds_task(self):
task = Task(task_id='task_1', name='my task')
db.session.add(task)
db.session.commit()
I think I was a little quick to post the question, but I hope this might help others come up with other ideas on how to troubleshoot a similar issue. 我想我很快就发布了这个问题,但是我希望这可以帮助其他人提出其他有关如何解决类似问题的想法。
In my src/models.py
file where I keep my models, you must make sure that your models are defined correctly. 在保存模型的
src/models.py
文件中,必须确保正确定义了模型。 Since Flask-SQLAlchemy is a wrapper around the SQLAlchemy you must use the data types under the db
object. 由于Flask-SQLAlchemy是SQLAlchemy的包装器,因此必须使用
db
对象下的数据类型。
Essentially, I had my models defined as such: 本质上,我的模型定义如下:
class Task(db.Model):
__tablename__ = 'tasks'
id = Column(Integer, primary_key=True)
task_id = Column(String)
name = Column(String)
created_at = Column(DateTime, default=datetime.datetime.now)
As you can see, I was inheriting from db.Model
instead of the return value of declarative_base()
. 如您所见,我从
db.Model
继承,而不是从declarative_base()
的返回值继承。 I also needed to add the db.
我还需要添加
db.
in front of all the data types, including Column
, Integer
, String
, Float
, DateTime
, relationship
, and ForeignKey
. 在所有数据类型之前,包括
Column
, Integer
, String
, Float
, DateTime
, relationship
和ForeignKey
。
So, I was able to fix my issue by changing my model to something like: 因此,我可以通过将模型更改为以下内容来解决问题:
class Task(db.Model):
__tablename__ = 'tasks'
id = db.Column(db.Integer, primary_key=True)
task_id = db.Column(db.String)
name = db.Column(db.String)
created_at = db.Column(db.DateTime, default=datetime.datetime.now)
See: Documentation on declaring Flask-SQLAlchemy models 请参阅: 有关声明Flask-SQLAlchemy模型的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.