我已经看到很多类似于此处发布的问题,但我没有发现任何帮助。我正在尝试使用Flask-Testing和Flask-Fixtures编写单元测试。我遇到了问题,因为Flask-Fixtures在传递给它的数据库上调用create_all
,在这种情况下,它实际上并不创建表。我检查db.metadata.tables.keys
这表明所有型号的表创建使用db.Model
Flask-SQLAlchemy create_all不起作用
config.py
:
from flask_api import FlaskAPI
from flask.ext.sqlalchemy import SQLAlchemy
from .models import db
app = FlaskAPI(__name__)
app.config.from_object('app.test_config')
ctx = app.app_context()
ctx.push()
db.init_app(app)
db.create_all()
答:当我在做这个
import os
from config import BASE_DIR
# SQLALCHEMY_DATABASE_URI = 'sqlite:///test-db'
SQLALCHEMY_DATABASE_URI = 'sqlite:////Users/XXX/projects/XXX-api/app/inventory/test-db'
testing = True
debug = True
FIXTURES_DIRS = (
os.path.join(BASE_DIR, "inventory/"),
)
表的创建失败vars(db.metadata)
给出这个:
{'_bind': None,
'_fk_memos': defaultdict(list, {}),
'_schemas': set(),
'_sequences': {},
'naming_convention': immutabledict({'ix': 'ix_%(column_0_label)s'}),
'schema': None,
'tables': immutabledict({'gr_merchant_ftp_info': Table('gr_merchant_ftp_info', MetaData(bind=None), Column('id', BigInteger(), table=<gr_merchant_ftp_info>, primary_key=True, nullable=False), Column('merchant_id', BigInteger(), table=<gr_merchant_ftp_info>), Column('chain_id', BigInteger(), table=<gr_merchant_ftp_info>), Column('hostname', String(length=128), table=<gr_merchant_ftp_info>, nullable=False), Column('username', String(length=128), table=<gr_merchant_ftp_info>, nullable=False), Column('password', String(length=128), table=<gr_merchant_ftp_info>, nullable=False), Column('path', String(length=512), table=<gr_merchant_ftp_info>, nullable=False), Column('install_ts', DateTime(timezone=True), table=<gr_merchant_ftp_info>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11007dd10>, for_update=False)), Column('parsed_file_base_path', String(length=256), table=<gr_merchant_ftp_info>), schema=None), 'gr_article_product_mapping': Table('gr_article_product_mapping', MetaData(bind=None), Column('id', BigInteger(), table=<gr_article_product_mapping>, primary_key=True, nullable=False, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11006fc10>, for_update=False)), Column('product_id', BigInteger(), table=<gr_article_product_mapping>), Column('article_id', String(length=128), table=<gr_article_product_mapping>), Column('install_ts', DateTime(timezone=True), table=<gr_article_product_mapping>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11007d090>, for_update=False)), Column('store_code', String(length=128), table=<gr_article_product_mapping>), Column('conversion_factor', Float(precision=53), table=<gr_article_product_mapping>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11007d210>, for_update=False)), schema=None), 'gr_store_merchant_mapping': Table('gr_store_merchant_mapping', MetaData(bind=None), Column('id', BigInteger(), table=<gr_store_merchant_mapping>, primary_key=True, nullable=False, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x10f67f790>, for_update=False)), Column('merchant_id', BigInteger(), table=<gr_store_merchant_mapping>), Column('store_id', BigInteger(), table=<gr_store_merchant_mapping>), Column('install_ts', DateTime(timezone=True), table=<gr_store_merchant_mapping>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11007d650>, for_update=False)), Column('store_code', String(length=128), table=<gr_store_merchant_mapping>), schema=None)})}
任何指针将不胜感激,谢谢!
单元测试是如此有用,但这是一种屁股疼痛的屁股。我无法从您的问题中判断是否存在实际的堆栈跟踪,警告或此处发生的无声故障。我也不知道你的ORM模型是否可以自己工作,但当你尝试使用它们进行单元测试时会失败。 – AlexLordThorsen
这是'失败'的默默,我想通了。 – madhukar93